如何从数组中获取数值,就像使用.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