如何從數組中獲取數值,就像使用.map一樣?這裏是我的代碼:如何獲取Ruby中數組的最後一個元素?
counter = 0
ary = Array.new
puts "How many teams do you have to enter?"
hm = gets.to_i
until counter == hm do
puts "Team City"
city = gets.chomp
puts "Team Name"
team = gets.chomp
ary.push([city, team])
counter += 1
end
ary.map { |x, y|
puts "City: #{x} | Team: #{y}"
}
print "The last team entered was: "
ary.last
最終的結果看起來是這樣的:
City: Boston | Team: Bruins
City: Toronto | Team: Maple Leafs
The last team entered was:
=> ["Toronto", "Maple Leafs"]
但我想最後一行讀
The last team entered was: Toronto Maple Leafs
我如何獲得該行我的價值觀,而不=>括號和引號?
起初我所有「Nawwww我喜歡我的格式」,然後我嘗試了你的,哦,是它的wayyyy更好。媽的點燃,fam!我只是在學Ruby,這很棒。謝謝! – ComputerUser5243
'ary.map {| x,y |放置「城市:#{x} |團隊:#{y}」}'濫用'map',必須在那裏使用'each'迭代器**。 – mudasobwa