【问题标题】:ruby - if string is not contained within an arrayruby - 如果字符串不包含在数组中
【发布时间】:2010-12-21 20:29:05
【问题描述】:

我只想在这里输出一个锚点。如果 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

【问题讨论】:

    标签: ruby haml each conditional-operator staticmatic


    【解决方案1】:
    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
    

    【讨论】:

    • 对不起,伙计们-这比我意识到的要多,只是按照您的建议做,我得到了多次出现的除非或!代码(在本例中为锚)
    【解决方案2】:

    好的,所以我认为我们正在检查 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" }
    

    抱歉 - 我误解了你在做什么......以为你在做一个包含?在一个数组上,但它是一个字符串... :-)

    【讨论】:

    • 虽然pastie.org/721159 给了我一个“无法将字符串转换为数组”的错误,但这听起来是正确的
    • 馅饼不是我的意思 - 如果在块之外放置第二个...顺便说一下 - current_page 是什么类型?是字符串吗?
    • current_page 确实是一个字符串
    • 把这个放在 each 里面,它迭代和传递 each_string,做一些与 each_string 无关的事情在我看来是错误的......我在这里错过了什么?
    • 我唯一要添加的是一个标志,found_item,如果 page_options[:pre_flash] 中的任何项目是current_page 的子字符串,它就会被设置。块的两种用途是相关的,因为它们在同一个集合上操作 - 但一种用途是设置标志,另一种是显示 - 所以是否应该在单独的块中执行此操作是一个有争议的问题......
    猜你喜欢
    • 2015-05-24
    • 2020-02-14
    • 2021-01-30
    • 2021-05-30
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多