Creating Ruby Hashes

I just created a quick Ruby screencast on creating hashes. I don’t cover all of the possibilities, but I do show 5 ways—including one you should never use. Let me know what you think, and if you have questions or ideas for a future screencast, chime in!

Ruby Hashes from David Brady on Vimeo.

7 thoughts on “Creating Ruby Hashes

  1. David Brady Post author

    Heh, I’m not even sure what that means… In cryptography jargon, when you say Alice and Bob everyone knows you’re talking about two people who want to have an encrypted conversation. Carol and Dave are two more people, used in examples when you need more than 2 people. After that, everybody else has a name that matches their role: Trent Trustworthy, Eve Eavesdropper, Mallory Malicious, Peggy Prover, Victor Verifier, etc. Or you can just continue alternating boy and girl names (Eddie, Fran, George, Harriet) like hurricanes. :-)

    Reply
  2. David Brady Post author

    Hah, you’re actually getting ahead of me. :-) I left that one out for now, only because it was the weird Kernel#Hash method instead of standard Hash.new. However, James Edward Gray II has also taken me to task for this, and we’re going to record a followup to it tomorrow. I’m really looking forward to it–I’ll let you know as soon as it’s up!

    Reply
  3. David Brady Post author

    Yeah, the inject->hash builder is a bit advanced for the level I was aiming at, but it’s a good tool. I just finished a followup screencast with James Edward Gray II, and he showed me how to build a hash like this in Ruby 1.9, and it’s MUCH cleaner. Definitely worth checking out. I should have that video up tomorrow. Thanks!

    Reply
  4. David Brady Post author

    Yep! I know it, but wanted to hold it back here because this screencast was more aimed at beginners. Also, though, don’t you think that the inject method is just a little bit gratuitously clever? I use it rarely enough that it hasn’t moved up to an immediately recognizable pattern in my brain so I don’t think of it as idiomatic.

    Ooh, be sure to check out the followup screencast I did with James Edward Gray II on Associative Arrays and Ruby Hashes in 1.9, at about 19:55 he shows off the each_with_object method in 1.9. It makes all of the gratuitousness of the inject method go away, but leaves all of the cleverness and elegance. It’s really slick.

    Thanks!

    Reply
  5. David Brady Post author

    I will sometimes (ab)use maps for this:

    irb(main):005:0> ray = (0..2).map { [0] * 3}
    => [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    irb(main):006:0> ray[1][1] = 9
    => 9
    irb(main):007:0> ray
    => [[0, 0, 0], [0, 9, 0], [0, 0, 0]]

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s