روبي - إذا كانت السلسلة غير موجودة ضمن مجموعة
-
22-07-2019 - |
سؤال
أريد فقط أن الناتج مرساة واحد هنا.إذا كان current_page في مجموعة أحصل على اثنين (.html -nf.html).إذا لم يكن في مجموعة أنا الحصول على العديد من المراسي كما أن هناك عناصر في المصفوفة.
أنا باستخدام 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
المحلول
unless current_page.include? the_string
تحرير:
هل يمكن كسر كل حلقة إذا كنت تريد الخاص بك أولا العثور على أن تكون واحدة فقط.ولكن الآن يبدو هذا غريبا, لأنك بالتكرار على مجموعة
وكسر بعد أول عنصر بغض النظر عما يحدث.أنا معالجة المشكلة ؟
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
نصائح أخرى
حسنا، لذلك أعتقد أننا التحقق من أن أيا من page_options [: CURRENT_INDEX]. هي سلاسل فرعية من 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" }
وعذرا - I يساء فهم ما كنت تفعل ... أعتقد أنك كانوا يفعلون التضمين؟ على مجموعة لكنها كانت سلسلة ...: -)
لا تنتمي إلى StackOverflow