我正在尝试编写一个脚本,通过在文本文件中使用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
,但它只显示第一行的结果而没有警告。
答案 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。