爲什麼在Ruby中這是一個語法錯誤?在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-09 23:06:38
逗號在行尾,而不是在開頭。 – shime 2013-04-09 23:10:43
Ruby中不需要這種扭曲,忽略尾隨逗號。 – 2013-04-09 23:24:25