From: Eric Hodel Date: 2005-10-28T13:39:19+09:00 Subject: Re: difference between the each iterator and the collect iterator? On Oct 27, 2005, at 8:17 PM, vasten@gmail.com wrote: > 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 } > and > [1, 2, 3, 4].each{|i| puts i } > > Can some guru in this group shed some light? $ ri Enumerable#collect ----------------------------------------------------- Enumerable#collect enum.collect {| obj | block } => array enum.map {| obj | block } => array ------------------------------------------------------------------------ Returns a new array with the results of running _block_ once for every element in _enum_. (1..4).collect {|i| i*i } #=> [1, 4, 9, 16] (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"] -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04