I have an issue dealing with a hash of objects. My hashes are player names, and the object has a property @name
also.
I am trying to iterate over multiple players, and be able to use their methods and such with rather clean code. Here is how I create the hash:
puts "Who all is playing?"
gets.split.each do |p|
players[p] = Player.new(p)
end
And then I want to iterate through the players like this, but it doesn't work.
players.each_key do |p_name, obj|
puts obj.name + " turn"
However, this does work:
players.each_key do |p_name, obj|
puts players[p_name].name + " turn"
On the first try, I get an error of obj being nil. Can someone explain why this won't work, and how to do it the way I would like to?
Thanks!
P.S. What is the correct terminology for the array or hash that I made? I come from PHP, so I think of it as an array (with hashes), but it technically isn't an array in Ruby?