From: "David A. Black" Date: 2007-11-04T05:28:48+09:00 Subject: Re: x=[]; x[:bla][:some_key] does not work? Hi -- On Sun, 4 Nov 2007, Joshua Muheim wrote: > 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! Are you sure it's not giving you a "Symbol as array index" error? > What wrong here? A couple of things. You're indexing an array with a symbol, but you have to use an integer. You're also expecting a non-existent array element to be something other than nil, but it isn't: a = [] a[1] # nil You're then trying (or would be, if you used an integer instead of :bla) to call the method [] on nil, and nil has no such method. (Remember that the index syntax, a[n], is actually a method call: a.[](n).) 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!