0

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?

1
  • Terminology tends to differ from language to language. Arrays in PHP are technically an 'ordered map', but can be used as an array, hash table, dictionary, stack, etc. In Ruby, Array and Hash are two distinct classes. One is for integer-indexed collection, and the other for key-value pairs. Their usage is very similar to PHP, except when using shorthand, [] refers to an array, and {} refers to a hash. Commented Aug 5, 2010 at 21:33

2 Answers 2

3

You want to use each_pair and not each_key. The method each_key only gives one argument to the block, which is the key. Therefore your obj remain unbound. each_pair on the other hand gives you both the key and the corresponding value.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh that is perfect! Is there really any reason to use each_key for anything then? (in general, as oppose to .each)
@phoffer: each and each_pair are synonyms. If you only want to access the keys, there is a (minor) performance benefits to use each_key instead of each/each_pair
Ok then. The performance is negligible to me, but it is easiest to use each_pair, and that is why I am working in Ruby.
3

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?

It's called a Hash in Ruby, and it is not the same as an array. Hashes are created with:

my_hash = Hash.new

or

my_hash = {}

While arrays are done thusly:

my_array = Array.new

or

my_array = []

Ruby hashes are associative arrays, like "arrays" in PHP. Ruby arrays are more like traditional "C" arrays, in that they're indexed with integers.

1 Comment

Thank you for that. I was kind of thinking along those lines, but I was just really unsure of it. players = {} is what I had done to create my hash. I am glad you understand what I meant by I come from PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.