settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
Jak mogę zrobić:Ruby rozpakować tablica do blokowania
settings.each do |ip, port|
...
end
Zamiast:
settings.each do |config|
ip, port = *config
...
end
settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
Jak mogę zrobić:Ruby rozpakować tablica do blokowania
settings.each do |ip, port|
...
end
Zamiast:
settings.each do |config|
ip, port = *config
...
end
Twój pierwszy przykład działa, ponieważ Ruby będzie destructure argumenty blokowych. Zobacz to article, aby uzyskać więcej informacji na temat destrukturyzacji w Ruby.
Sposób szukasz jest Array # mapa
settings = [ ['127.0.0.1', 80], ['0.0.0.0', 443] ]
settings.map { |ip, port| puts "IP: #{ip} PORT: #{port}" }
który powróci
# => // IP: 127.0.0.1 PORT: 80
# // => IP: 0.0 .0.0 PORT: 443
Po prostu to zrób. Będzie działać tak, jak Ty. – sawa