Forum: Ruby n-dimensional array in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Christian (Guest)
on 2008-11-01 02:10
(Received via mailing list)
Hi,

i have a little Java problem, I want to solve using Ruby. Therefor I
need an n-dimensional array. In Java it looks like this:

double dArray[][][] = new double[x.length()+1][y.length()+1][x.length()
+y.length()+3];
dArray[0][0][0] = 0;
dArray[0][0][1] = POSITIVE_INFINITY;

Further values will be computed through loops and written into the
array.

How do I initialize such an array in Ruby?
Michael G. (Guest)
on 2008-11-01 02:31
(Received via mailing list)
On Fri, Oct 31, 2008 at 8:09 PM, Christian 
<removed_email_address@domain.invalid>
wrote:
> Further values will be computed through loops and written into the
> array.
>
> How do I initialize such an array in Ruby?
>
Infinity = 1.0/0

a = [[],[],[]]
a[0][0][0] = 0
a[0][0][1] = Infinity

The arrays will grow to whatever size you need them to.

HTH,
Michael G.
Mike G. (Guest)
on 2008-11-01 02:46
Christian wrote:
> i have a little Java problem, I want to solve using Ruby. Therefor I
> need an n-dimensional array.

def Array.multi(*dimensions)
  dimensions.reverse.inject(nil) { |result, dim|
    Array.new(dim) { result ? result.dup : nil }
  }
end

a = Array.multi(3, 4, 5)

p a[0]     #=> [[nil, nil, ...], ... ]
p a[0][0]  #=> [nil, nil, nil, nil, nil]

a[0][0][0] = :fred
a[2][3][4] = :barney

p a[0][0][0]  #=> :fred
p a[2][3][4]  #=> :barney

a[3][0][0]
# => exception: undefined method `[]' for nil; index past dim size
Mike G. (Guest)
on 2008-11-01 11:05
Mike G. wrote:
> def Array.multi(*dimensions)
>   dimensions.reverse.inject(nil) { |result, dim|
>     Array.new(dim) { result ? result.dup : nil }
>   }
> end

My mistake.  Array#dup is not a recursive deep copy.

def Array.multi(*dimensions)
  dimensions.reverse.inject(nil) { |result, dim|
    Array.new(dim) { Marshal.load(Marshal.dump(result)) }
  }
end
Brian A. (Guest)
on 2008-11-01 18:45
(Received via mailing list)
Mike G. <removed_email_address@domain.invalid> writes:

>   dimensions.reverse.inject(nil) { |result, dim|
>     Array.new(dim) { Marshal.load(Marshal.dump(result)) }
>   }
> end

Yearning for macros...
Brian A. (Guest)
on 2008-11-01 18:46
(Received via mailing list)
Michael G. <removed_email_address@domain.invalid> writes:

>>
>> Further values will be computed through loops and written into the
>> array.
>>
>> How do I initialize such an array in Ruby?
>>
> Infinity = 1.0/0

~$ irb
irb(main):001:0> X = 7
=> 7
irb(main):002:0> Infinity = 1.0/0
=> Infinity
irb(main):003:0> 0 / Infinity
=> 0.0
irb(main):004:0> Infinity * Infinity
=> Infinity
irb(main):005:0> Beyond = Infinity
=> Infinity
irb(main):006:0> 2 * Infinity and Beyond
=> Infinity
irb(main):007:0> Infinity.infinite?
=> 1

Cool - I just learned about Infinity in Ruby. Hey Dave T., maybe
you could add a bit about Infinity in the next relase of Programming
Ruby or Programming Ruby 1.9
Michael G. (Guest)
on 2008-11-01 19:04
(Received via mailing list)
On Sat, Nov 1, 2008 at 12:43 PM, Brian A. <removed_email_address@domain.invalid>
wrote:
>>> dArray[0][0][0] = 0;
> irb(main):001:0> X = 7
> => Infinity
> irb(main):007:0> Infinity.infinite?
> => 1
>
> Cool - I just learned about Infinity in Ruby. Hey Dave T., maybe
> you could add a bit about Infinity in the next relase of Programming
> Ruby or Programming Ruby 1.9
>
Yeah, I don't think I've seen it discussed in any books before, but I
could be wrong.

I stumbled upon it accidentally one day when playing with ruby and
it's rules for division.

Michael G.
This topic is locked and can not be replied to.