From: "Berger, Daniel" Date: 2005-10-27T02:23:07+09:00 Subject: Re: Nuby Array questions - each iterates nested arrays? > -----Original Message----- > From: Ed Howland [mailto:ed.howland@gmail.com] > Sent: Wednesday, October 26, 2005 11:17 AM > To: ruby-talk ML > Subject: Nuby Array questions - each iterates nested arrays? > > > a=[[1, 2, 3], [4, 5, 6]] > => [[1, 2, 3], [4, 5, 6]] > a.length > => 2 > a.each {|x| puts x} > 1 > 2 > 3 > 4 > 5 > 6 > => [[1, 2, 3], [4, 5, 6]] > a.each_index {|x| puts x} > 0 > 1 > > Why does each traverse into the inner arrays as well (tried > it with 3 and 4 deep?) Array#length seems to be correct to me > and Array#each_index This seems to happen to collect!, and > I'd gather other methods as well. Actually, it doesn't. It just looks that way because of the way 'puts' works with arrays. Try 'ruby -e "puts [1,2,3]"' to see what I mean. See the docs on IO.puts for more details. To see what's really happening replace 'puts' with 'p' (inspect) in your example. Regards, Dan