ruby - se la stringa non è contenuta in un array
-
22-07-2019 - |
Domanda
Voglio solo produrre un'ancora qui. Se current_page è nella matrice, ne ottengo due (.html e -nf.html). Se non si trova nell'array ottengo tante ancore quanti sono gli elementi nell'array.
Sto usando 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
Soluzione
unless current_page.include? the_string
Modifica
Potresti interrompere ogni ciclo se vuoi che la tua prima scoperta sia l'unica.
Ma ora sembra un po 'strano, perché stai iterando su un array
e rompere dopo il primo elemento, qualunque cosa accada.
Sto affrontando il tuo problema?
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
Altri suggerimenti
Okay, quindi penso che stiamo controllando che nessuna delle page_options [: current_index] sia una sottostringa di 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" }
Mi dispiace - ho capito male cosa stavi facendo ... pensavi che stessi facendo un include? su un array ma era una stringa ... :-)