Hi all,
I'm a Ruby noob so please forgive me...
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 = Array.new(3,3)" -> I 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 à la
"int arr = 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.
the big difference is that there is no difference between compile time and
runtime in ruby so, if i could say
a = array[3]
in ruby, then
a = array[3][3]
would mean
a = (the_first_array = array[3])[ index = 3]
in otherwords you'd index the first array by the second value. another issue
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, switches
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 the
"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 = 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
···
On Tue, 11 Oct 2005, Peter v. N. wrote:
--
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
===============================================================================