From: "Morris, Chris" Date: 2002-03-27T00:31:39+09:00 Subject: RE: yield in iterator's body - > So, my question is, what exactly is the difference between 'each' and > 'collect'? > Why doesn't the 'each' block return the value of the last expression? collect returns the newly built array. each simply returns the original array. ---------------------------------------------------------- Array#collect arr.collect {| obj | block } -> anArray ------------------------------------------------------------------------ Returns a new array by invoking block once for every element, passing each element as a parameter to block. The result of block is used as the given element in the new array. See also Array#collect!. a = [ "a", "b", "c", "d" ] a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"] a #=> ["a", "b", "c", "d"] ------------------------------------------------------------- Array#each arr.each {| item | block } -> arr ------------------------------------------------------------------------ Calls block once for each element in arr, passing that element as a parameter. a = [ "a", "b", "c" ] a.each {|x| print x, " -- " } produces: a -- b -- c --