Forum: Ruby self nesting array [[...]]

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.
48d1aca7191f2d16e184971054c7c143?d=identicon&s=25 Meinrad Recheis (Guest)
on 2006-06-10 11:22
(Received via mailing list)
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 :P

>> 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
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-06-10 13:35
(Received via mailing list)
Meinrad Recheis 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.
48d1aca7191f2d16e184971054c7c143?d=identicon&s=25 Meinrad Recheis (Guest)
on 2006-06-10 13:48
(Received via mailing list)
On 6/10/06, Florian Groß <florgro@gmail.com> 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.
D812408537ac3a0fa2fec96eb8811559?d=identicon&s=25 John Carter (johncarter)
on 2006-06-13 03:25
(Received via mailing list)
On Sat, 10 Jun 2006, Meinrad Recheis 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 Weirich make your head ache...
   http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...


John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
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.
D812408537ac3a0fa2fec96eb8811559?d=identicon&s=25 John Carter (johncarter)
on 2006-06-13 04:53
(Received via mailing list)
On Sat, 10 Jun 2006, Meinrad Recheis 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 Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
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.
This topic is locked and can not be replied to.