Enumerable module

Hi,

I've just downloaded Ruby and I'm looking for the source code for
the Enumerable module (which I'm interested in as it seems to be a
very elegant way of achieving Smalltalk-like collection
functionality). Unfortunately i can't find it (I was looking for
something like a file called Enumerable.rb). Is it not there, or
implemented in C or something?

···

--
"It's easier to find people online who openly support the KKK than
people who openly support the RIAA" -- comment on Wikipedia
(Email: zen19725 at zen dot co dot uk)

Yup. See enum.c
-Charlie

···

On Jun 19, 2004, at 5:38 PM, phil hunt wrote:

Hi,

I've just downloaded Ruby and I'm looking for the source code for
the Enumerable module (which I'm interested in as it seems to be a
very elegant way of achieving Smalltalk-like collection
functionality). Unfortunately i can't find it (I was looking for
something like a file called Enumerable.rb). Is it not there, or
implemented in C or something?

"phil hunt" <zen19725@zen.co.uk> schrieb im Newsbeitrag
news:slrncd9nes.2f7.zen19725@cabalamat.cabalamat.org...

Hi,

I've just downloaded Ruby and I'm looking for the source code for
the Enumerable module (which I'm interested in as it seems to be a
very elegant way of achieving Smalltalk-like collection
functionality). Unfortunately i can't find it (I was looking for
something like a file called Enumerable.rb). Is it not there, or
implemented in C or something?

The basic principle is simple: define some method signatures with semantics
(for Enumerable is just each() which is expected to receive a block that is
handed over element after element in the collection) and implement a module
by having all methods rely on the set of defined method signatures.

A rather trivial example:

module EnumerableMath
  def sum
    s = 0
    each {|x| s += x}
    s
  end

  def prod
    p = 1
    each {|x| p *= x}
    p
  end
end

class TestContainer
  include EnumerableMath

  def initialize(from, to, step = 1)
    raise ArgumentError unless ((to - from) <=> 0) == (step <=> 0)
    @from, @to, @step = from, to, step
  end

  def each
    e = @from

    if @step > 0
      while e <= @to
        yield e
        e += @step
      end
    else
      while e >= @to
        yield e
        e += @step
      end
    end

    self
  end
end

numbers = TestContainer.new( 1, 10, 1 )

p numbers.sum
p numbers.prod

Kind regards

    robert

Hi,

If you are looking for the implementation, it is part of the
ruby-interpreter. You can find the source in enum.c.
It very clearly written and easy to understand.

Regards,
Kristof

···

On Sun, 20 Jun 2004 01:44:15 +0100, phil hunt wrote:

Hi,

I've just downloaded Ruby and I'm looking for the source code for
the Enumerable module (which I'm interested in as it seems to be a
very elegant way of achieving Smalltalk-like collection
functionality). Unfortunately i can't find it (I was looking for
something like a file called Enumerable.rb). Is it not there, or
implemented in C or something?

Is that for efficiency, or because it wouldn't be possible to
implement it in Ruby?

···

On Sun, 20 Jun 2004 11:54:50 +0900, Charles Mills <cmills@freeshell.org> wrote:

On Jun 19, 2004, at 5:38 PM, phil hunt wrote:

Hi,

I've just downloaded Ruby and I'm looking for the source code for
the Enumerable module (which I'm interested in as it seems to be a
very elegant way of achieving Smalltalk-like collection
functionality). Unfortunately i can't find it (I was looking for
something like a file called Enumerable.rb). Is it not there, or
implemented in C or something?

Yup. See enum.c

--
"It's easier to find people online who openly support the KKK than
people who openly support the RIAA" -- comment on Wikipedia
(Email: zen19725 at zen dot co dot uk)

Quoteing zen19725@zen.co.uk, on Mon, Jun 21, 2004 at 05:33:17AM +0900:

>> functionality). Unfortunately i can't find it (I was looking for
>> something like a file called Enumerable.rb). Is it not there, or
>> implemented in C or something?
>Yup. See enum.c

Is that for efficiency, or because it wouldn't be possible to
implement it in Ruby?

Efficiency, its one of the most commonly used modules in ruby.

Sam