# Self nesting array \[\[...\]\]

**URL:** <https://www.ruby-forum.com/t/self-nesting-array/95717>\
**Category:** Ruby\
**Created:** [June 10, 2006, 11:22am UTC](https://www.ruby-forum.com/t/self-nesting-array/95717 "2006-06-10T11:22:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [June 10, 2006, 11:22am UTC](https://www.ruby-forum.com/t/self-nesting-array/95717/1 "2006-06-10T11:22:15Z")

</div>

Dear list,

I had the crazy idea to infinitely nest an array. At first I did  
expect to get an overflow of some sort or a strang error message when  
trying to inspect it but none of it happened:

> > a=[]; a[0]=a  
> > =\> [[…]]
> 
> > a[0]  
> > =\> [[…]]
> 
> > a[0][0]  
> > =\> [[…]]
> 
> > a[0][0][0]  
> > =\> [[…]]

… I had just created a self nesting array.

Inspect is clever enough to prevent itself from running into an  
infinite loop. You can’t measure this array’s depth. The paradox thing  
about it is, that it’s nested infinitely deep but is only made up of  
one Array 😉

I know that cyclic references are no big deal but it’s fun to think  
about what you can do with them. Actually not much. But you can …

… test the robustness of some of Array’s methods and some libraries ^^

> > a.to\_s  
> > =\> “[…]”

hehe, to\_s is clever too.

> > a.flatten  
> > ArgumentError: tried to flatten recursive array  
> > from (irb):107:in `flatten’  
> > from (irb):107  
> > from :0

flatten is checking for recursion.

> > a.uniq  
> > SystemStackError: stack level too deep  
> > from (irb):112:in `hash’  
> > from (irb):112  
> > from :0

oops. uniq didn’t check 😛

> > require “yaml”  
> > =\> true
> 
> > a.to\_yaml  
> > =\> “— !ruby/object:Array {}”
> 
> > s=a.to\_yaml  
> > =\> “— !ruby/object:Array {}”
> 
> > YAML.load s  
> > =\> []

YAML does not return a self nested array again.  
Even more interesting is that marshalling a self nested array is  
possible:

> > s=Marshal.dump a  
> > =\> “\004\010[\006@\000”
> 
> > Marshal.load s  
> > =\> [[…]]

Well, actually this post doesn’t make much sense. It’s just for  
entertainment.

have a nice weekend,  
– henon

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [June 10, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/self-nesting-array/95717/2 "2006-06-10T13:35:49Z")

</div>

Meinrad R. wrote:

> > > require “yaml”  
> > > =\> true
> > 
> > > a.to\_yaml  
> > > =\> “— !ruby/object:Array {}”
> > 
> > > s=a.to\_yaml  
> > > =\> “— !ruby/object:Array {}”
> > 
> > > YAML.load s  
> > > =\> []

Works here:

> irb(main):001:0\> a = []; a \<\< a  
> =\> [[…]]  
> irb(main):002:0\> require ‘yaml’  
> =\> true  
> irb(main):003:0\> a.to\_yaml  
> =\> “— &id001 \n- \*id001”  
> irb(main):004:0\> YAML.load(a.to\_yaml)  
> =\> [[…]]

YAML::VERSION is 0.60. RUBY\_VERSION 1.8.2.

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [June 10, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/self-nesting-array/95717/3 "2006-06-10T13:48:09Z")

</div>

On 6/10/06, Florian Groß [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> YAML::VERSION is 0.60. RUBY\_VERSION 1.8.2.
> 
> –  
> [http://flgr.0x42.net/](http://flgr.0x42.net/)

you are right, it works for me too. must have made an error before.

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [June 13, 2006, 4:53am UTC](https://www.ruby-forum.com/t/self-nesting-array/95717/4 "2006-06-13T04:53:55Z")

</div>

On Sat, 10 Jun 2006, Meinrad R. wrote:

> I had the crazy idea to infinitely nest an array.

While we having harmless, silly, gibbering fun, how about…

irb  
irb(main):001:0\> a={}  
=\> {}  
irb(main):002:0\> a[a]=a  
=\> {{…}=\>{…}}  
irb(main):003:0\> a[a][a][a][a][a][a]  
=\> {{…}=\>{…}}  
irb(main):004:0\>

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

Carter’s Clarification of Murphy’s Law.

“Things only ever go right so that they may go more spectacularly wrong  
later.”

From this principle, all of life and physics may be deduced.

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [June 13, 2006, 3:25am UTC](https://www.ruby-forum.com/t/self-nesting-array/95717/5 "2006-06-13T03:25:31Z")

</div>

On Sat, 10 Jun 2006, Meinrad R. wrote:

> I had the crazy idea to infinitely nest an array. At first I did  
> expect to get an overflow of some sort or a strang error message when  
> trying to inspect it but none of it happened:
> 
> > > a=[]; a[0]=a

Since you have a taste for such things…

Let Jim W. make your head ache…  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20469](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20469)

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

Carter’s Clarification of Murphy’s Law.

“Things only ever go right so that they may go more spectacularly wrong  
later.”

From this principle, all of life and physics may be deduced.
