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| i2 }
=> [2, 4]
irb(main):012:0> m.collect! { |i| i2 }
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.
···
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| i2 }
=> [2, 4]
irb(main):012:0> m.collect! { |i| i2 }
NoMethodError: undefined method `collect!’ for #<M:0x1892d0>
from (irb):12
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| i2 }
=> [2, 4]
irb(main):012:0> m.collect! { |i| i2 }
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.
that’s why “Does collect! (or map!) make sense for Enumerables?”,
but you expressed it so much better
···
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.
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
···
On Thu, Jan 15, 2004 at 01:11:41AM +0900, Robert Klemme wrote: