为什么这是ruby中的语法错误?
#!/usr/bin/ruby
servers = [
"xyz1-3-l"
, "xyz1-2-l"
, "dws-zxy-l"
, "abcl"
]
hostname_input = ARGV[0]
hostname = hostname_input.gsub( /.example.com/, "" )
servers.each do |server|
if hostname == server then
puts "that's the one"
break
end
end..。当我执行这个脚本时,我得到了这个输出...
$ ./test.rb abc1
./test.rb:5: syntax error, unexpected ',', expecting ']'
, "xyz1-2-l"
^
./test.rb:6: syntax error, unexpected ',', expecting $end
, "dws-zxy-l"
^..。如果我只是把所有的东西都放在同一行上,那就没问题了。
$ cat test.rb
#!/usr/bin/ruby
servers = [ "xyz1-3-l" , "xyz1-2-l" , "dws-zxy-l" , "abcl" ]
hostname_input = ARGV[0]
hostname = hostname_input.gsub( /.example.com/, "" )
servers.each do |server|
if hostname == server then
puts "that's the one"
break
end
end
$ ./test.rb dws-zxy-l
that's the one发布于 2013-04-10 10:00:51
听着,没有逗号(或引号):
servers = %W[
xyz1-3-l
xyz1-2-l
dws-zxy-l
abcl
]
# => ["xyz1-3-l", "xyz1-2-l", "dws-zxy-l", "abcl"] 发布于 2013-04-10 07:24:14
换行符在Ruby中很重要。您需要将逗号放在行尾,或者在换行符之前使用反斜杠来表示该行正在继续(当然,在这种情况下,将逗号移到下一行有什么意义?)。
https://stackoverflow.com/questions/15914108
复制相似问题