ruby: si la cadena no está contenida dentro de una matriz
-
22-07-2019 - |
Pregunta
Solo quiero generar un ancla aquí. Si la página actual está en la matriz, obtengo dos (.html y -nf.html). Si no está en la matriz, obtengo tantos anclajes como elementos en la matriz.
Estoy 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
Solución
unless current_page.include? the_string
Editar:
Podría romper cada bucle si desea que su primer hallazgo sea el único.
Pero ahora esto parece un poco extraño, porque estás iterando sobre una matriz
y romper después del primer elemento, pase lo que pase.
¿Estoy abordando su problema en absoluto?
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
Otros consejos
Bien, entonces creo que estamos comprobando que ninguna de las opciones de página [: índice_corriente] son ??subcadenas de página_corriente.
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" }
Lo siento, no entendí lo que estabas haciendo ... ¿creías que estabas haciendo una inclusión? en una matriz pero era una cadena ... :-)