紧凑,如果OR语句没有触发

时间:2016-08-19 13:43:58

标签: ruby

灵感来自this我现在

hostname = %x{hostname}

if ['staging', 'prod', 'jlpc'].include? hostname
  puts "yes"
end

我的Linux主机名为jlpc但不打印yes

为什么这不起作用?

2 个答案:

答案 0 :(得分:5)

%x{hostname}
# => "hostname\n"

解决方案:

puts "yes" if ['staging', 'prod', 'jlpc'].include? hostname.chomp

答案 1 :(得分:3)

我在我的机器上试过,该行在我的主机名末尾添加\n。所以,试试

hostname = %x{hostname}.rstrip