DISQUS

DISQUS Hello! Majestic Sea Creature is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

http://blog.majesticseacreature.com/archives/2008.08/first_post.html

Started by sandal · 11 months ago

No excerpt available. Jump to website »

4 comments

  • Hey, Greg, in self.build_archive_indices(entries) wouldn't this be sufficient?

    archives = Hash.new { [] }
  • Unfortunately, no, that won't work.

    The code you have shown will return an array, that's true, but it never assigns it to the Hash. So a new array will be built every time:

    >> a = Hash.new { [] }
    => {}
    >> a[:b]
    => []
    >> a[:b] << 1
    => [1]
    >> a[:b]
    => []

    This is why Hash.new passes the hash itself and the key in question into the block. You need to do something with that to persist any data.
  • Oh, right. You'd have to do archives[e.archive] <<= e as well.
  • I don't think I've ever seen anyone use <<= before, the fact that it works is basically an accident.

    It expands out to:

    obj = obj << other

    Which is very confusing: "Append to this object, then re-assign it"

    In this case, if you used that, you'd just be working around the fact that your Hash.new { .. } definition was written incorrectly.

    Hash.new { |h,k| h[k] = [] } is idiomatic for these purposes :)

Add New Comment

Returning? Login