X=[]; x[:bla][:some_key] does not work?

On Sun, 04 Nov 2007 06:38:52 -0500, dblack wrote:

sepp2k’s answer is the only correct answer in this thread.
I didn’t say it didn’t. I’m still wondering what you found in my posts
that was incorrect, if you wouldn’t mind sharing.

David

OK. I see you’ve got the semantics right, but you didn’t point out the
solution.

–Ken

OK. I see you’ve got the semantics right, but you didn’t point out the
solution.

In other words, nothing he said was wrong, but he still should have
said something different?

Think about it. When you made this post:

sepp2k’s answer is the only correct answer in this thread.

…you doomed this whole thread to bickering. Don’t be doing that.
Play it cool, and you get to have much better conversations. Otherwise
this place just turns into programming.reddit.com, or the Rails list,
or something like that.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

On 3-Nov-07, at 4:44 PM, Sebastian H. wrote:

blk = lambda {|h,k| h[k] = Hash.new(&blk)}
x = Hash.new(&blk)
x[:la][:li][:lu][:chunky][:bacon][:foo] = “bar”

On 4-Nov-07, at 4:34 AM, Jimmy K. wrote:

a = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }

Very nice, Sebastian and Jimmy.

Leading to the obvious…

class NestedHash < Hash
def initialize
blk = lambda {|h,k| h[k] = NestedHash.new(&blk)}
super(&blk)
end
end

class Hash
def Hash.new_nested_hash
Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
end
end

… and with no comment on propriety.

I’ve been using Ruby for years and I’m still startled by what you can
do when passing blocks to initializers. A few weeks ago I stumbled on
this:

Hash.new{ | h, k | h[k] = Array.new }

which is just a hash of arrays, completely trivial really, but I’d
never thought of it before, and now I’m using it all over the place.

And I was really impressed with Ruby when I discovered using blocks
with gsub.

Cheers,
Bob


Bob H. – tumblelog at
http://www.recursive.ca/so/
Recursive Design Inc. – weblog at
http://www.recursive.ca/hutch
http://www.recursive.ca/ – works on
http://www.raconteur.info/cms-for-static-content/home/

On Nov 5, 2007, at 5:27 AM, David A. Black wrote:

My favorite is:

class NonexistentKeyError < StandardError
end

hash = Hash.new {|h,k| raise NonexistentKeyError, “No such key: #
{k}” }

An alternate solution:

a = Hash.new { |h,k| h.fetch k }

David’s solution is nice because the message includes
the key. Maybe Ruby’s IndexError message should
report the unknown key.

Gary W.

Hi –

On Mon, 5 Nov 2007, Bob H. wrote:

I’ve been using Ruby for years and I’m still startled by what you can do when
passing blocks to initializers. A few weeks ago I stumbled on this:

Hash.new{ | h, k | h[k] = Array.new }

which is just a hash of arrays, completely trivial really, but I’d never
thought of it before, and now I’m using it all over the place.

My favorite is:

class NonexistentKeyError < StandardError
end

hash = Hash.new {|h,k| raise NonexistentKeyError, “No such key: #{k}”
}

:slight_smile:

David