From: Meinrad Recheis Date: 2006-06-10T18:21:45+09:00 Subject: [Fun] 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 ;-) 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