使用.include查找字符串?在ruby代码中与IRB结果不同

时间:2017-08-10 00:19:29

标签: ruby-on-rails ruby

在irb中,我可以按照我的期望进行跟进:

irb(main):001:0> "    end     ".include? "  end  " => true
irb(main):002:0> "    end     ".include? "end" => true
irb(main):003:0> "   vendor     ".include? "end" => true
irb(main):004:0> "   vendor     ".include? " end " => false

如果我将以下代码的“end”嵌入名为" line"的变量中,

if line.include? "end"
  puts "true when 'end'"
else
  puts "false when 'end'"
end
if line.include? "  end  "
  puts "true when '  end  '"
else
  puts "false when '  end  '"
end

我得到以下结果,正如我在IRb中所期望的那样:

Line:      end

true when 'end'
false when '  end  '

0 个答案:

没有答案