# N-dimensional array in Ruby

**URL:** <https://rubytalk.org/t/n-dimensional-array-in-ruby/49937>\
**Category:** ruby-talk\
**Created:** [1 November 2008 00:09 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937 "2008-11-01T00:09:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian4](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@Christian4](https://rubytalk.org/u/Christian4)\
**Post date:** [1 November 2008 00:09 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/1 "2008-11-01T00:09:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)\
**Post date:** [1 November 2008 00:29 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/2 "2008-11-01T00:29:59Z")

</div>

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 Guterl

> **···**
>
> On Fri, Oct 31, 2008 at 8:09 PM, Christian \<stadeschuldt@googlemail.com\> wrote:
> 
> > 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?

---

<div class="post-metadata">

**Author:** ![Mike\_Gold](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Mike\_Gold](https://rubytalk.org/u/Mike_Gold)\
**Post date:** [1 November 2008 00:45 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/3 "2008-11-01T00:45:27Z")

</div>

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)  
&nbsp;&nbsp;dimensions.reverse.inject(nil) { |result, dim|  
&nbsp;&nbsp;&nbsp;&nbsp;Array.new(dim) { result ? result.dup : nil }  
&nbsp;&nbsp;}  
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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mike\_Gold](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Mike\_Gold](https://rubytalk.org/u/Mike_Gold)\
**Post date:** [1 November 2008 09:04 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/4 "2008-11-01T09:04:06Z")

</div>

Mike Gold wrote:

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

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

def Array.multi(\*dimensions)  
&nbsp;&nbsp;dimensions.reverse.inject(nil) { |result, dim|  
&nbsp;&nbsp;&nbsp;&nbsp;Array.new(dim) { Marshal.load(Marshal.dump(result)) }  
&nbsp;&nbsp;}  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [1 November 2008 16:43 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/5 "2008-11-01T16:43:57Z")

</div>

Michael Guterl \<mguterl@gmail.com\> writes:

> > 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?
> 
> 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 Thomas, maybe  
you could add a bit about Infinity in the next relase of Programming  
Ruby or Programming Ruby 1.9

> **···**
>
> > On Fri, Oct 31, 2008 at 8:09 PM, Christian \<stadeschuldt@googlemail.com\> wrote:
> 
> > a = [,,]  
> > a[0][0][0] = 0  
> > a[0][0][1] = Infinity
> > 
> > The arrays will grow to whatever size you need them to.
> > 
> > HTH,  
> > Michael Guterl

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [1 November 2008 16:43 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/6 "2008-11-01T16:43:55Z")

</div>

Mike Gold \<mike.gold.4433@gmail.com\> writes:

> Mike Gold wrote:
> 
> > def Array.multi(\*dimensions)  
> > &nbsp;&nbsp;dimensions.reverse.inject(nil) { |result, dim|  
> > &nbsp;&nbsp;&nbsp;&nbsp;Array.new(dim) { result ? result.dup : nil }  
> > &nbsp;&nbsp;}  
> > end
> 
> My mistake. Array#dup is not a recursive deep copy.
> 
> def Array.multi(\*dimensions)  
> &nbsp;&nbsp;dimensions.reverse.inject(nil) { |result, dim|  
> &nbsp;&nbsp;&nbsp;&nbsp;Array.new(dim) { Marshal.load(Marshal.dump(result)) }  
> &nbsp;&nbsp;}  
> end

Yearning for macros...

---

<div class="post-metadata">

**Author:** ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)\
**Post date:** [1 November 2008 17:03 UTC](https://rubytalk.org/t/n-dimensional-array-in-ruby/49937/7 "2008-11-01T17:03:19Z")

</div>

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 Guterl

> **···**
>
> On Sat, Nov 1, 2008 at 12:43 PM, Brian Adkins \<lojicdotcom@gmail.com\> wrote:
> 
> > Michael Guterl \<mguterl@gmail.com\> writes:
> > 
> > > On Fri, Oct 31, 2008 at 8:09 PM, Christian \<stadeschuldt@googlemail.com\> wrote:
> > > 
> > > > 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?
> > > 
> > > 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 Thomas, maybe  
> > you could add a bit about Infinity in the next relase of Programming  
> > Ruby or Programming Ruby 1.9
