Question about magic enumerators in > 1.8

Discussion in 'Ruby' started by dblack@wobblini.net, Jul 9, 2006.

  1. Guest

    Hi --

    Can someone jog my memory, and/or enlighten me, as to what a case
    would be where it's useful to have:

    array.map

    return an Enumerator? I can't seem to come up with an example where:

    array.map.anything

    is different from

    array.anything

    Thanks --


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
    Rails developers
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    => me
     
    , Jul 9, 2006
    #1
    1. Advertising

  2. On Jul 9, 2006, at 9:03 AM, wrote:

    > Hi --
    >
    > Can someone jog my memory, and/or enlighten me, as to what a case
    > would be where it's useful to have:
    >
    > array.map
    >
    > return an Enumerator? I can't seem to come up with an example where:
    >
    > array.map.anything
    >
    > is different from
    >
    > array.anything
    >
    > Thanks --


    Well, you can use it to roll the oft-requested map_with_index():

    array.each_with_index.map { |obj, i| .. }

    That's not a map() example though, so maybe not what you wanted.

    James Edward Gray II
     
    James Edward Gray II, Jul 9, 2006
    #2
    1. Advertising

  3. Guest

    Hi --

    On Sun, 9 Jul 2006, James Edward Gray II wrote:

    > On Jul 9, 2006, at 9:03 AM, wrote:
    >
    >> Hi --
    >>
    >> Can someone jog my memory, and/or enlighten me, as to what a case
    >> would be where it's useful to have:
    >>
    >> array.map
    >>
    >> return an Enumerator? I can't seem to come up with an example where:
    >>
    >> array.map.anything
    >>
    >> is different from
    >>
    >> array.anything
    >>
    >> Thanks --

    >
    > Well, you can use it to roll the oft-requested map_with_index():
    >
    > array.each_with_index.map { |obj, i| .. }
    >
    > That's not a map() example though, so maybe not what you wanted.


    Right, it isn't :) I'm still looking for a map example.


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
    Rails developers
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    => me
     
    , Jul 9, 2006
    #3
  4. On Jul 9, 2006, at 10:36 AM, wrote:

    > Hi --
    >
    > On Sun, 9 Jul 2006, James Edward Gray II wrote:
    >
    >> On Jul 9, 2006, at 9:03 AM, wrote:
    >>
    >>> Hi --
    >>> Can someone jog my memory, and/or enlighten me, as to what a case
    >>> would be where it's useful to have:
    >>>
    >>> array.map
    >>> return an Enumerator? I can't seem to come up with an example
    >>> where:
    >>>
    >>> array.map.anything
    >>> is different from
    >>>
    >>> array.anything
    >>> Thanks --

    >>
    >> Well, you can use it to roll the oft-requested map_with_index():
    >>
    >> array.each_with_index.map { |obj, i| .. }
    >>
    >> That's not a map() example though, so maybe not what you wanted.

    >
    > Right, it isn't :) I'm still looking for a map example.
    >
    >
    > David
    >
    > --
    > http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    > http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
    > Rails developers
    > http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    > => me
    >


    Contrived example: I know a method I'm about to call iterates thru me
    with each doing something. It's the last thing it does and what the
    results of the thing it does rather than myself at the end:

    some_method(array.map)

    Ok so it's a REALLY contrived example.
     
    Logan Capaldo, Jul 10, 2006
    #4
  5. ts Guest

    >>>>> "d" == dblack <> writes:

    d> return an Enumerator? I can't seem to come up with an example where:

    d> array.map.anything

    d> is different from

    d> array.anything

    moulon% ./ruby -e '[12].map.with_index {}'
    moulon%

    moulon% ./ruby -e '[12].with_index {}'
    -e:1: undefined method `with_index' for [12]:Array (NoMethodError)
    moulon%


    p.s.:
    :))))


    --

    Guy Decoux
     
    ts, Jul 10, 2006
    #5
  6. Guest

    Hi --

    On Mon, 10 Jul 2006, Christian Neukirchen wrote:

    > writes:
    >
    >> Hi --
    >>
    >> Can someone jog my memory, and/or enlighten me, as to what a case
    >> would be where it's useful to have:
    >>
    >> array.map
    >>
    >> return an Enumerator? I can't seem to come up with an example where:
    >>
    >> array.map.anything
    >>
    >> is different from
    >>
    >> array.anything

    >
    > names.map { |x| x.size }.max, and you don't need an immediate array.


    That's not an example of what I'm asking about, though. There's no
    enumerator returned there.


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
    Rails developers
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    => me
     
    , Jul 10, 2006
    #6
    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. Matt Taylor
    Replies:
    6
    Views:
    442
    Victor Bazarov
    Jul 13, 2004
  2. Replies:
    6
    Views:
    4,195
    Pete Becker
    Apr 23, 2005
  3. Dave
    Replies:
    1
    Views:
    321
    Ron Natalie
    Nov 9, 2005
  4. Giles Bowkett
    Replies:
    9
    Views:
    416
    Giles Bowkett
    Dec 17, 2007
  5. Omran Nazir

    Newbie question: Enumerators

    Omran Nazir, Nov 21, 2009, in forum: Ruby
    Replies:
    4
    Views:
    159
    Omran Nazir
    Nov 22, 2009
Loading...

Share This Page