Forum: Ruby Dummy question about method in class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2009-02-18 16:36
Dear all

I have a dummy question.

[1,2,3,4,5].inject { |sum,n| sum+n }
The inject method in this case is to calculate the sum of array.

I searched the RDoc. Why the inject method is belongs to Enumerable
Class, but not Array Class?

Many thanks
Valentino
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2009-02-18 16:43
(Received via mailing list)
On Feb 18, 2009, at 4:36 PM, Valentino Lun wrote:
>
> [1,2,3,4,5].inject { |sum,n| sum+n }
> The inject method in this case is to calculate the sum of array.
>
> I searched the RDoc. Why the inject method is belongs to Enumerable
> Class, but not Array Class?

Enumerable is a Module, not a Class. It can be mixed into other
classes, so the Class gains the Methods of the module. Arrays uses
Enumerable instead of implementing #inject on its own, so they respond
to Enumerables #inject.

As a short presentation, i redefine Enumerable#inspect to do something
different (never try this at home!):

===
module Enumerable
   def inject
     puts "hey"
   end
end

[].inject #=> puts "hey" now
===

Regards,
Florian Gilcher

--
Florian Gilcher

smtp:   flo@andersground.net
jabber: Skade@jabber.ccc.de
gpg:    533148E2
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-18 22:52
(Received via mailing list)
On 18.02.2009 16:36, Valentino Lun wrote:
> [1,2,3,4,5].inject { |sum,n| sum+n }
> The inject method in this case is to calculate the sum of array.

Additional to Florian's good explanation: be aware that the version you
posted has a special property: it will return nil for empty Enumerables.

Consider this version which in most cases is the one you want (an empty
list summed up results in 0):

[1,2,3,4,5].inject(0) { |sum,n| sum+n }

Kind regards

  robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-02-19 10:13
Valentino Lun wrote:
> I have a dummy question.
>
> [1,2,3,4,5].inject { |sum,n| sum+n }
> The inject method in this case is to calculate the sum of array.
>
> I searched the RDoc. Why the inject method is belongs to Enumerable
> Class, but not Array Class?

The Enumerable module can be shared by many classes. Any class which
implements its own 'each' method can mixin Enumerable and gain a whole
load of functionality.

For example:

class Fib
  include Enumerable
  def initialize(a,b,count)
    @a, @b, @count = a, b, count
  end
  def each
    @count.times do
      yield @a
      @a, @b = @b, @a+@b
    end
  end
end

p Fib.new(1,1,10).inspect
p Fib.new(1,1,10).max
p Fib.new(1,1,10).inject(0) { |sum,n| sum+n }
This topic is locked and can not be replied to.