collect! and Enumerable

Discussion in 'Ruby' started by Jim Freeze, Jan 14, 2004.

  1. Jim Freeze

    Jim Freeze Guest

    Hi

    I just noticed that Enumerable does not support collect! although
    it does support collect. Was this changed from earlier versions?
    If so, why?

    irb(main):001:0> class M
    irb(main):002:1> def each
    irb(main):003:2> yield 1
    irb(main):004:2> yield 2
    irb(main):005:2> end
    irb(main):006:1> end
    => nil
    irb(main):007:0> class M
    irb(main):008:1> include Enumerable
    irb(main):009:1> end
    => M
    irb(main):010:0> m=M.new
    => #<M:0x1892d0>
    irb(main):011:0> m.collect { |i| i*2 }
    => [2, 4]
    irb(main):012:0> m.collect! { |i| i*2 }
    NoMethodError: undefined method `collect!' for #<M:0x1892d0>
    from (irb):12

    Thanks

    --
    Jim Freeze
    ----------
    "When in doubt, tell the truth."
    -- Mark Twain
    Jim Freeze, Jan 14, 2004
    #1
    1. Advertising

  2. On Thu, Jan 15, 2004 at 12:47:24AM +0900, Jim Freeze wrote:
    > Hi
    >
    > I just noticed that Enumerable does not support collect! although
    > it does support collect. Was this changed from earlier versions?
    > If so, why?
    >
    > irb(main):001:0> class M
    > irb(main):002:1> def each
    > irb(main):003:2> yield 1
    > irb(main):004:2> yield 2
    > irb(main):005:2> end
    > irb(main):006:1> end
    > => nil
    > irb(main):007:0> class M
    > irb(main):008:1> include Enumerable
    > irb(main):009:1> end
    > => M
    > irb(main):010:0> m=M.new
    > => #<M:0x1892d0>
    > irb(main):011:0> m.collect { |i| i*2 }
    > => [2, 4]
    > irb(main):012:0> m.collect! { |i| i*2 }
    > NoMethodError: undefined method `collect!' for #<M:0x1892d0>
    > from (irb):12


    Does collect! (or map!) make sense for Enumerables? Keep in mind that
    collect! (aka map!) is defined in Array.
    I guess you could define it as
    module Enumerable
    def collect!(&b)
    to_a.collect!(&b)
    end
    end
    but I don't feel too bad about having to do to_a explicitly.

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    There are 3 kinds of people: those who can count & those who can't.
    -- Unknown source
    Mauricio Fernández, Jan 14, 2004
    #2
    1. Advertising

  3. "Mauricio Fernández" <> schrieb im Newsbeitrag
    news:-stuttgart.de...
    > On Thu, Jan 15, 2004 at 12:47:24AM +0900, Jim Freeze wrote:
    > > Hi
    > >
    > > I just noticed that Enumerable does not support collect! although
    > > it does support collect. Was this changed from earlier versions?
    > > If so, why?
    > >
    > > irb(main):001:0> class M
    > > irb(main):002:1> def each
    > > irb(main):003:2> yield 1
    > > irb(main):004:2> yield 2
    > > irb(main):005:2> end
    > > irb(main):006:1> end
    > > => nil
    > > irb(main):007:0> class M
    > > irb(main):008:1> include Enumerable
    > > irb(main):009:1> end
    > > => M
    > > irb(main):010:0> m=M.new
    > > => #<M:0x1892d0>
    > > irb(main):011:0> m.collect { |i| i*2 }
    > > => [2, 4]
    > > irb(main):012:0> m.collect! { |i| i*2 }
    > > NoMethodError: undefined method `collect!' for #<M:0x1892d0>
    > > from (irb):12

    >
    > Does collect! (or map!) make sense for Enumerables? Keep in mind that
    > collect! (aka map!) is defined in Array.
    > I guess you could define it as
    > module Enumerable
    > def collect!(&b)
    > to_a.collect!(&b)
    > end
    > end
    > but I don't feel too bad about having to do to_a explicitly.


    Especially since your definition of collect! would defy users'
    expectations that it would do it in place while it really creates a copy.
    :)

    Regards

    robert
    Robert Klemme, Jan 14, 2004
    #3
  4. [OT] Re: collect! and Enumerable

    On Thu, Jan 15, 2004 at 01:11:41AM +0900, Robert Klemme wrote:
    > > Does collect! (or map!) make sense for Enumerables? Keep in mind that
    > > collect! (aka map!) is defined in Array.
    > > I guess you could define it as
    > > module Enumerable
    > > def collect!(&b)
    > > to_a.collect!(&b)
    > > end
    > > end
    > > but I don't feel too bad about having to do to_a explicitly.

    >
    > Especially since your definition of collect! would defy users'
    > expectations that it would do it in place while it really creates a copy.
    > :)


    that's why "Does collect! (or map!) make sense for Enumerables?",
    but you expressed it so much better ;-)

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    And Bruce is effectively building BruceIX
    -- Alan Cox
    Mauricio Fernández, Jan 14, 2004
    #4
  5. Re: [OT] Re: collect! and Enumerable

    "Mauricio Fernández" <> schrieb im Newsbeitrag
    news:-stuttgart.de...
    > On Thu, Jan 15, 2004 at 01:11:41AM +0900, Robert Klemme wrote:
    > > > Does collect! (or map!) make sense for Enumerables? Keep in mind

    that
    > > > collect! (aka map!) is defined in Array.
    > > > I guess you could define it as
    > > > module Enumerable
    > > > def collect!(&b)
    > > > to_a.collect!(&b)
    > > > end
    > > > end
    > > > but I don't feel too bad about having to do to_a explicitly.

    > >
    > > Especially since your definition of collect! would defy users'
    > > expectations that it would do it in place while it really creates a

    copy.
    > > :)

    >
    > that's why "Does collect! (or map!) make sense for Enumerables?",
    > but you expressed it so much better ;-)


    Err, no. Apparently you wanted to say the same as I did but I was tricked
    into believing something else by "I guess you could define it as ...".
    Sounded like a positive suggestion to me. Now, who said English was easy?
    :)

    Kind regards

    robert
    Robert Klemme, Jan 14, 2004
    #5
  6. On Thursday, January 15, 2004, 2:47:24 AM, Jim wrote:

    > Hi


    > I just noticed that Enumerable does not support collect! although
    > it does support collect. Was this changed from earlier versions?
    > If so, why?


    A. collect always returns an array.

    B. Bang methods should not change the class of the receiver.
    Gavin Sinclair, Jan 14, 2004
    #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. Daniel Sheppard
    Replies:
    0
    Views:
    96
    Daniel Sheppard
    Sep 16, 2005
  2. Gregory Brown
    Replies:
    4
    Views:
    118
    Gregory Brown
    Nov 28, 2005
  3. John Maclean

    immutable and enumerable?

    John Maclean, Feb 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    93
    Robert Klemme
    Feb 12, 2006
  4. Andre Nathan

    Enumerable#all? and empty arrays

    Andre Nathan, Aug 9, 2006, in forum: Ruby
    Replies:
    6
    Views:
    92
    Just Another Victim of the Ambient Morality
    Aug 10, 2006
  5. Replies:
    8
    Views:
    102
    Dave Burt
    Oct 1, 2006
Loading...

Share This Page