[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01835] enum examples?

From: Clemens Hintze <clemens.hintze@...>
Date: 2000-03-15 13:37:33 UTC
List: ruby-talk #1835
Hugh Sasse Staff Elec Eng writes:
> Has anyone any examplse of using the Enumerable module?  I've had a 
> look around, but not found much.  I'm thinking of something to allow
> me to have names for bits in a flag field, so I can test if one or
> more is set.

Hi,

if I have not misunderstood your question, I think you have
misunderstood the purpose of module Enumerable ;-)))

Enumerable offers some methods like: min, max, sort, ... that could be
included into any class that provide the method 'each'. After
including, the class get all methods of Enumerable for free ...

Classes Array or IO of the standard lib, include Enumerable for
example. Here is a another one:

   class MyRange
      include Enumerable     # This is important!!!
   
      def initialize(start, stop, step)
         @start = start
         @stop = stop
         @step = step
      end

      def each
         i = @start
         while i < @stop
            yield i
            i += @step
         end
      end
   end
   
   ra = MyRange.new(1,10,2)
   
   print "Max element of 'ra' is: ", ra.max
   print "Instance 'ra' converted to an Array: "
   p ra.to_a

You can see, although there is no method 'max' or 'to_a' defined for
MyRange, any instance can use them, because they are coming from
Enumerable. As you can include more than one module, it is somewhat
similar with multiple inheritance. But cleaner, IMHO.

> I could create my own object for this, of course, but
> why re-invent unless I must?

Here, I think, it would not be re-invention but invention. There is no
such class, AFAIK.

> 	Hugh
> 	hgs@dmu.ac.uk

\cle

In This Thread