使用数组

时间:2015-12-28 01:11:43

标签: ruby

我遇到了这个案例陈述的问题,情况如下 subDomainBlock是来自其他函数的多行字符串,它们就像

"fo-ds-ats.member.g02.yahoodns.net has address 98.139.21.169
fo-ds-ats.member.g02.yahoodns.net has IPv6 address 2001:4998:58:2201::50   
Host wiki.yahoo.com not found: 3(NXDOMAIN)"

完整的功能涉及

def data_filter(subDomainBlock) 
        line = subDomainBlock.split("\n")
            #puts  " yo soy #{line.class}"
            line.each do | choise | 
            puts "-------------------"# puts "#{choise}#{choise.class}" 
            case choise 
            when choise.include?('not found')
                nil 
            when choise.include?('has address')
                puts "tiene una di"
            when choise.include?('is an alias')
                puts "es un alias"  
            when choise.include?('IPv6')
            else
                puts choise  

            end
        end     
    end

我也尝试过#{choise.include?('XXXXXXX')}但不起作用。

3 个答案:

答案 0 :(得分:0)

您无法在choise条件中使用when变量。 不要使用case-when,请使用if-elsif

答案 1 :(得分:0)

不要将变量传递给case条件

       case 
       when choise.include?('not found')
         nil 
       when choise.include?('has address')
         puts "tiene una di"
       when choise.include?('is an alias')
         puts "es un alias"  
       when choise.include?('IPv6')
       else
         puts choise  
       end

答案 2 :(得分:0)

您可以对when语句使用正则表达式匹配器。

def data_filter(subDomainBlock) 
  line = subDomainBlock.split("\n")
  line.each do |choise| 
    case choise 
    when /not found/
      nil 
    when /has address/
      puts "tiene una di"
    when /is an alias/
      puts "es un alias"  
    when /IPv6/
    else
      puts choise  
    end
  end     
end