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

K

Ken Bloom

Hi --



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
 
G

gilesb

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 Bowkett

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

hutch

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


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
 
D

dblack

Hi --

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}" }

:)


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
G

gwtmp01

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 Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top