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
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.
···
--
http://flgr.0x42.net/
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/ruby-talk/20469
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.
···
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
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.
···
On Sat, 10 Jun 2006, Meinrad Recheis wrote:
I had the crazy idea to infinitely nest an array.
you are right, it works for me too. must have made an error before.
···
On 6/10/06, Florian Groß <florgro@gmail.com> wrote:
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.
--
http://flgr.0x42.net/