代码之家  ›  专栏  ›  技术社区  ›  Dr. Frankenstein

ruby-如果数组中不包含字符串

  •  1
  • Dr. Frankenstein  · 技术社区  · 16 年前

    我只想在这里输出一个锚。如果当前页面在数组中,我得到两个(.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
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Steinbitglis    16 年前
    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
  •  1
  •   Dafydd Rees    16 年前

    好的,我想我们正在检查page_选项[:current_index]都不是当前_页面的子字符串。

    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" }
    

    推荐文章