From: mow@... (Morus Walter) Date: 2005-10-28T14:02:05+09:00 Subject: Re: difference between the each iterator and the collect iterator? In article <1130469173.146954.208420@g49g2000cwa.googlegroups.com>, vasten@gmail.com writes: > Hi: > I am a newbie as far as Ruby is concerned. I was going through the > programming ruby book (2nd Edition), but was not able to figure out the > difference between the 2 statements: > > [1, 2, 3, 4].collect{[i] puts i } I guess that should read [1, 2, 3, 4].collect{|i| puts i } > and > [1, 2, 3, 4].each{|i| puts i } > > Can some guru in this group shed some light? > run it in irb and you'll see, that your collect statement returns an array of nils ( [nil, nil, nil, nil] ) while the each statement returns the initial array ( [1, 2, 3, 4] ) I'm not a guru but AFAIK this is since collect returns an array created from the return values of the block (puts returns nil) while each returns the array it's called on. HTH Morus