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

x=; x[:bla][:some_key] does not work?
Posted by Joshua Muheim (josh) on 03.11.2007 21:21
Hi all

PHP lets me easily create multidimensional hashes using the following
syntax:

x = array();
x["bla"]["some_key"] = true;

Is Ruby not capable of doing this?

x =
x[:bla][:some_key] = true

gives me a nil error!

What wrong here?

How about this approach:

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

a[2][1]=2
a[2][2][3]=4
a[3][1][1][1]=1

p a #=> {2=>{1=>2, 2=>{3=>4}}, 3=>{1=>{1=>{1=>1}}}}

( http://snippets.dzone.com/posts/show/4146 )

Cheers,

j. k.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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

Hi --

···

On Sun, 4 Nov 2007, Ken Bloom wrote:

On Sat, 03 Nov 2007 20:46:00 -0500, dblack wrote:

Hi --

On Sun, 4 Nov 2007, Ken Bloom wrote:

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

Could you point out the mistakes in my answers for me? I'd be
interested. Thanks --

David

The Hash.new constructor has several different behaviors depending on
what you pass it:

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

--
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!

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

--Ken

···

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

Hi --

On Sun, 4 Nov 2007, Ken Bloom wrote:

On Sat, 03 Nov 2007 20:46:00 -0500, dblack wrote:

Hi --

On Sun, 4 Nov 2007, Ken Bloom wrote:

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

Could you point out the mistakes in my answers for me? I'd be
interested. Thanks --

David

The Hash.new constructor has several different behaviors depending on
what you pass it:

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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

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/