Self nesting array [[...]]

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 :wink:

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 :stuck_out_tongue:

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

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.

On 6/10/06, Florian Groß [email protected] wrote:

YAML::VERSION is 0.60. RUBY_VERSION 1.8.2.


http://flgr.0x42.net/

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

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 : [email protected]
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.

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

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
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.