# X=\[\]; x\[:bla\]\[:some\_key\] does not work?

**URL:** <https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897>\
**Category:** ruby-talk\
**Created:** [3 November 2007 20:21 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897 "2007-11-03T20:21:13Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jimmy\_Kofler](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@Jimmy\_Kofler](https://rubytalk.org/u/Jimmy_Kofler)\
**Post date:** [4 November 2007 09:34 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897/21 "2007-11-04T09:34:54Z")

</div>

> 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](http://snippets.dzone.com/posts/show/4146) )

Cheers,

j. k.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [5 November 2007 19:35 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897/22 "2007-11-05T19:35:28Z")

</div>

An alternate solution:

&nbsp;&nbsp;&nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;class NonexistentKeyError \< StandardError  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;hash = Hash.new {|h,k| raise NonexistentKeyError, "No such key: #{k}" }

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [4 November 2007 11:38 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897/23 "2007-11-04T11:38:52Z")

</div>

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:  
> &nbsp;&nbsp;&nbsp;\* Advancing With Rails, Edison, NJ, November 6-9  
> &nbsp;&nbsp;&nbsp;\* Advancing With Rails, Berlin, Germany, November 19-22  
> &nbsp;&nbsp;&nbsp;\* Intro to Rails, London, UK, December 3-6 (by Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details!

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [4 November 2007 16:10 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897/24 "2007-11-04T16:10:03Z")

</div>

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/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [4 November 2007 19:09 UTC](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897/25 "2007-11-04T19:09:59Z")

</div>

> 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](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

[Previous page](https://rubytalk.org/t/x-x-bla-some-key-does-not-work/41897.md?page=1)
