From: "Ara.T.Howard" Date: 2005-10-11T22:25:53+09:00 Subject: Re: multi-dimensional Arrays --8323328-1895834244-1129036049=:12197 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1895834244-1129036049=:12197" 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-1895834244-1129036049=:12197 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Tue, 11 Oct 2005, Peter v. N. wrote: > Hi all, > > I'm a Ruby noob so please forgive me... > > I know there exist "simple" ways (Google's thy friend) to create md array= s > with Ruby. But it's somehow cumbersome and it would be nice if it could b= e > done like "array =3D Array.new(3,3)" -> I know too that this means not th= e > 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]"? this is precisely because these languages do __not__ have multi-dimensional arrays and they muse be composed of uni-dimensional ones. ruby is the same= =2E the big difference is that there is no difference between compile time and runtime in ruby so, if i could say a =3D array[3] in ruby, then a =3D array[3][3] would mean a =3D (the_first_array =3D array[3])[ index =3D 3] in otherwords you'd index the first array by the second value. another iss= ue is that, in ruby, a declaration is the same as a definition. so, in c, you cannot write printf ("%d\n", (int [42])[0]); but in ruby you can write printf "%d\n", [42][0] because you can simoultaneously define and declare an object, and this have object, including array, literals. > Maybe because this approach is not strictly oo-oriented? But then, switch= es > and loops in Ruby are neither. i thinks it's more syntax/interpreter related. > It's just the devil inside who likes to know about the philosophy about t= he > "why it is done like that" and I'm not bitching about Ruby (by no means). the best reason may be that's it's so easy to setup you're own md definer: harp:~ > cat a.rb require 'pp' md =3D lambda{|*ds| Array::new(ds.shift||0).map{md[*ds] unless ds.empty?= }} pp md[] pp md[1] pp md[2] pp md[2,3] pp md[2,3,4] harp:~ > ruby a.rb [] [nil] [nil, nil] [[nil, nil, nil], [nil, nil, nil]] [[[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]], [[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]]] cheers. -a --=20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-1895834244-1129036049=:12197-- --8323328-1895834244-1129036049=:12197--