From: dblack@... Date: 2006-03-23T10:51:01+09:00 Subject: Re: questions/suggestions from a Ruby newbie --8323328-812446712-1143078644=:7076 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-812446712-1143078644=:7076" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-812446712-1143078644=:7076 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 23 Mar 2006, Simon Kr=F6ger wrote: > Welcome Diego, > > first let me say it may not be the wisest thing to introduce oneself to > a language group by suggesting to change that language. (this may > explain the relative low volume of replies you got) > >> -Is there a way to have multidimentional arrays in Ruby? Ie something >> like variable[3][2]? One could use arrays of arrays but it does not >> look quite as clear when reading the code. > > This comes up ever so often, and i don't get it. I think arrays of > arrays is the more flexible and general case. Perhaps this is enough to > make you happy? > > class MultiArray > def MultiArray.new *args > Array.new(args.shift) {args.empty? ? nil : MultiArray.new(*args)} > end > end > > usage: > > m =3D MultiArray.new(2, 3, 4) > m[1][2][3] =3D 'O' > p m > > #=3D>[[[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]], > [[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, "O"]]] > This reminds me of my favorite way to create an pseudo-auto-vivifying hash: Hash.new {|h,k| h[k] =3D h.dup } :-) I don't think there's an equally concise way for the array thing. David --=20 David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black --8323328-812446712-1143078644=:7076-- --8323328-812446712-1143078644=:7076--