difference between the each iterator and the collect iterator?

Discussion in 'Ruby' started by vasten@gmail.com, Oct 28, 2005.

  1. Guest

    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{ puts i }
    and
    [1, 2, 3, 4].each{|i| puts i }

    Can some guru in this group shed some light?

    Thanks.
     
    , Oct 28, 2005
    #1
    1. Advertising

  2. Morus Walter Guest

    In article <>,
    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{ 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
     
    Morus Walter, Oct 28, 2005
    #2
    1. Advertising

  3. Guest

    Collect is called for the return value:

    [1, 2, 3, 4].collect {|i| puts i } #=> [nil, nil, nil, nil]

    Another transformation might be:

    [1,2,3,4].collect {|i| i * 2 } #=> [2, 4, 6, 8]

    Each just returns the original collection, but we usually call each
    just as an iterator, while we call collect to get the transformed
    result.

    Cheers,
    Dave
     
    , Oct 28, 2005
    #3
  4. Eric Hodel Guest

    On Oct 27, 2005, at 8:17 PM, 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{ 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 - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
     
    Eric Hodel, Oct 28, 2005
    #4
  5. Selon :
    > 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{ puts i }
    > and
    > [1, 2, 3, 4].each{|i| puts i }
    >
    > Can some guru in this group shed some light?
    >
    > Thanks.
    >
    >


    #collect applies the block on each value of the array, and *returns the
    result of the expression as an array for each value*. #each applies the
    block on each value of the array, but *returns the unmodified array*.
    #collect is used for the effect it has on the array, #each is just used
    for looping.

    You can't see the difference here because you are not catching the array
    returned by #collect. Usually one would do:
    result = [1,2,3,4].collect{|i| do_something(i)}
    while leaving #each the way you did it.
    --
    Christophe Grandsire.

    http://rainbow.conlang.free.fr

    You need a straight mind to invent a twisted conlang.
     
    Christophe Grandsire, Oct 28, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,395
  2. Hendrik Maryns
    Replies:
    18
    Views:
    1,446
  3. Andreas S
    Replies:
    1
    Views:
    116
    Robert Klemme
    Oct 20, 2007
  4. Andreas S
    Replies:
    0
    Views:
    110
    Andreas S
    Oct 19, 2007
  5. James Black
    Replies:
    15
    Views:
    235
    James Black
    May 24, 2006
Loading...

Share This Page