settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
How can I do:
settings.each do |ip, port|
...
end
Instead of:
settings.each do |config|
ip, port = *config
...
end
settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
How can I do:
settings.each do |ip, port|
...
end
Instead of:
settings.each do |config|
ip, port = *config
...
end
您的第一个示例有效,因为 Ruby 将解构块参数。有关在 ruby 中解构的更多信息,请参阅这篇文章。
您正在寻找的方法是 Array#map
settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
settings.map { |ip, port| puts "IP: #{ip} PORT: #{port}" }
这将返回
#// => IP: 127.0.0.1 PORT: 80
#// => IP: 0.0.0.0 PORT: 443