From: "David A. Black" Date: 2005-10-11T21:35:44+09:00 Subject: Re: multi-dimensional Arrays --8323328-294073984-1129034136=:19017 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-294073984-1129034136=:19017" 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-294073984-1129034136=:19017 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Tue, 11 Oct 2005, Peter v. N. wrote: > Hi all, > > I'm a Ruby noob so please forgive me... Welcome to Ruby! > I know there exist "simple" ways (Google's thy friend) > to create md arrays with Ruby. But it's somehow cumbersome > and it would be nice if it could be done like "array =3D Array.new(3,3)" = -> I=20 > know too that this means not the same in Ruby... > > What I like to know: what led to the decission to do it that way > and not like it is done in the C/C++ (Java) languages =E0 la > "int[][] arr =3D new int[3][3]"? > > Maybe because this approach is not strictly oo-oriented? But then, > switches and loops in Ruby are neither. OK... but finding a couple of things that aren't strictly OO doesn't mean that Ruby should just give up and make everything non-OO :-) Each part of Ruby is carefully designed to contribute to the whole system. In the case of int[][] arr... Ruby variables are always untyped. If you can do: arr =3D [] you can also do: arr =3D "I am not an array!" The identifier 'arr' has no particular type. That's a big part of the dynamics and dynamism of Ruby. Also, you're assuming that [] means "array indexing". Actually, this: x[] is just syntactic sugar for this: x.[] # call method "[]" and you can define [] to do whatever you want: obj =3D Object.new # Define a "singleton" method [] for obj def obj.[](x) "Are you trying to access index #{x}??" end puts obj[3] # Are you trying to access index 3?? So there's no reason to think that the notation [][] would mean "nested array". There's a Matrix class, by the way, that might help you automate what you need to do a little more. David --=20 David A. Black dblack@wobblini.net --8323328-294073984-1129034136=:19017-- --8323328-294073984-1129034136=:19017--