ruby - si la chaîne n'est pas contenue dans un tableau
-
22-07-2019 - |
Question
Je veux seulement sortir une ancre ici. Si la current_page est dans le tableau j'en ai deux (.html et -nf.html). Si ce n'est pas dans le tableau, j'obtiens autant d'ancres qu'il y a d'éléments dans le tableau.
J'utilise StaticMatic .
- if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0
// loop through the pre-flash array and if current page matches, add -nf
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
- page_options[:pre_flash].each do |each_string|
- if current_page.include? each_string
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
Next
- else
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
Next
La solution
unless current_page.include? the_string
Modifier:
Vous pouvez briser chaque boucle si vous voulez que votre première découverte soit la seule.
Mais maintenant, cela semble un peu bizarre, parce que vous êtes en train de parcourir un tableau
et briser après le premier élément, peu importe ce qui se passe.
Est-ce que je m'adresse à votre problème?
options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
page_options[:pre_flash].each do |each_string|
if current_page.include? each_string
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
# This is the last ancor
break
else
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
# This is the last ancor
break
end
end
Autres conseils
D'accord, je pense donc que nous vérifions qu'aucune des options page_options [: current_index] ne soit une sous-chaîne de current_page.
if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0
found_item = false
// loop through the pre-flash array and if current page matches, add -nf
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
- page_options[:pre_flash].each do |each_string|
- if current_page.include? each_string
found_item = true
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
Next
# do whatever you need to get out of the staticmatic block...
- if !found_item
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
Désolé, j'ai mal compris ce que vous faisiez ... je pensais que vous faisiez un include? sur un tableau mais c'était une chaîne ...: -)