Ruby脚本逐行读取文件并使用puts执行if语句?

时间:2014-08-21 17:33:17

标签: ruby file if-statement line-by-line

我正在尝试编写一个脚本,通过在文本文件中使用if个参数来填写设备的映射。

以下是我尝试的代码:

input.txt中:

SWITCH
ROUTER
FIREWALL
ROUTER
ROUTER
FIREWALL
SWITCH
SWITCH

Ruby代码:

IO.foreach("input.txt") do |type_equipment|
  if type_equipment = "SWITCH"
    puts "SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch"
  elsif type_equipment = "ROUTER"
    puts "ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router"
  elsif type_equipment = "FIREWALL"
    puts "FIREWALL,BMC_COMPUTERSYSTEM,Network,Appliance,Firewall"
  end
end

代码运行但输出:

SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
main.rb:6: warning: found = in conditional, should be ==
main.rb:4: warning: found = in conditional, should be ==
main.rb:2: warning: found = in conditional, should be ==

我希望它评估文件中的每一行,如果它与if语句匹配,则输出关联的puts

我还为==语句绑定if,但它只显示第一行的结果而没有警告。

3 个答案:

答案 0 :(得分:3)

  • 请正确缩进您的代码
  • 如错误消息所示,您必须使用==代替=进行比较(=是变量分配)
  • type_equipment末尾有换行符,您可以使用String#chomp删除
  • 条件语句将最后一个表达式作为返回值,因此您可以在puts前面写一个if,而不是在块内重复它
  • 我建议使用case而不是重复的if语句

这些建议将导致:

IO.foreach("input.txt") do |type_equipment|
  puts case type_equipment.chomp
    when "SWITCH"   then "SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch"
    when "ROUTER"   then "ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router"
    when "FIREWALL" then "FIREWALL,BMC_COMPUTERSYSTEM,Network,Appliance,Firewall"
  end
end

答案 1 :(得分:2)

main.rb:6: warning: found = in conditional, should be ==

答案就在你面前。

比较您使用的两件事var1==var2

var1=var2是一个赋值运算符,它将var1的值设置为等于var2的值,因此您的代码应为:

IO.foreach("input.txt") do |type_equipment|
if type_equipment == "SWITCH"
  puts "SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch"
 elsif type_equipment == "ROUTER"
  puts "ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router"
 elsif type_equipment == "FIREWALL"
   puts "FIREWALL,BMC_COMPUTERSYSTEM,Network,Appliance,Firewall"
  end
end

答案 2 :(得分:0)

为了在添加新设备或停止使用现有设备时更容易维护代码,请考虑使用哈希:

hash = {"SWITCH" => "SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch",
        "ROUTER" => "ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router",
        "FIREWALL" => "FIREWALL,BMC_COMPUTERSYSTEM,Network,Appliance,Firewall"}

equipment =
"SWITCH
ROUTER
FIREWALL
ROUTER
PINGER
ROUTER
FIREWALL
SWITCH
SWITCH"

equipment.each_line do |l|
  equip = l.strip
  puts hash[equip] if hash.key?(equip)
end
  # SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
  # ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router
  # ...
  # SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch

或者,如果您愿意:

equipment.each_line do |l|
  equip = l.strip
  puts (hash.key?(equip) ? hash[equip] : "***What the heck is a '#{equip}'??")
end
  # SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch
  # ...
  # ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router
  # ***What the heck is a 'PINGER'??
  # ROUTER,BMC_COMPUTERSYSTEM,Network,Router,Ethernet Router
  # ...
  # SWITCH,BMC_COMPUTERSYSTEM,Network,Switch,Ethernet Switch

您可以使用String#chomp而不是String#strip,但我选择了后者,因为它会删除前后任何空格以及换行符。从文件中读取时,您需要使用枚举器IO#foreach