Community Page
- blog.majesticseacreature.com Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- Hey I did not even know it existed. But is it really legal to publish only part of a comment? Cheers Robert
- It's simple: block content from showing up referred by an about.com link.
- About.com can suck it
- This situation reminds me a bit of the recent brouhaha over <a href="http://allthingsd.com/">All Things Digital</a>'s practice of appropriating other folks' content,...
- I sure hope anyone working seriously in Ruby doesn't take about.com seriously. That's the worst thing though, they're capitalizing on the uninitiated. Whether they've been doing it...
Jump to original thread »
http://blog.majesticseacreature.com/archives/2008.08/first_post.html
Started by sandal · 11 months ago
8 months ago
archives = Hash.new { [] }
8 months ago
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.
8 months ago
8 months ago
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 :)