From: "David A. Black" Date: 2008-05-02T20:57:12+09:00 Subject: Re: Beginner Question on Ruby Hi -- On Fri, 2 May 2008, Arun Kumar wrote: > Hi all, > > I am beginning to learn ruby and cant seem to reason this behaviour. I > expect since empty or nill is the array, arr*n shouldnt change it. > > Why is this behaviour so? > > Code: > > num_arr = Array.new > puts "Num Alpha: #{num_arr} and size: #{num_arr.size}" > puts "Num Alpha: #{num_arr << num_arr} and size: #{num_arr.size}" > num_arr.insert(-1, num_arr << num_arr) > puts "Num Alpha: #{num_arr} and size: #{num_arr.size}" > > Output: > > Num Alpha: and size: 0 > Num Alpha: [...] and size: 1 > Num Alpha: [...][...][...][...][...][...][...][...][...] and size: 3 You're inserting an array into itself. Ruby represents that kind of recursive inclusion with dots. irb(main):005:0> a = [] => [] irb(main):006:0> a << a => [[...]] David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!