ruby - 如果字符串不包含在数组中
我只想在这里输出一个锚点。如果 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
I only want to output one anchor here. If the current_page is in the array I get two (.html and -nf.html). If it is not in the array I get as many anchors as there are items in the array.
I am using 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
如果您希望第一个发现是唯一的,您可以打破每个循环。
但现在这看起来有点奇怪,因为你正在迭代一个数组
无论发生什么,都会在第一个元素之后中断。
我完全解决了你的问题吗?
Edit:
You could break the each-loop if you want your first finding to be the only one.
But now this looks a little bit weird, because you are iterating over an array
and breaking after the first element no matter what happens.
Am I addressing your problem at all?
好的,所以我认为我们正在检查 page_options[:current_index] 中没有一个是 current_page 的子字符串。
抱歉 - 我误解了你在做什么...以为你在做一个包含?在数组上,但它是一个字符串......:-)
Okay, so I think we're checking that none of page_options[:current_index] are substrings of current_page.
Sorry - I misunderstood what you were doing... thought you were doing an include? on an array but it was a string... :-)