[#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:01839] Re: enum examples?

From: matz@... (Yukihiro Matsumoto)
Date: 2000-03-15 14:09:13 UTC
List: ruby-talk #1839
Hi,

In message "[ruby-talk:01834] enum examples?"
    on 00/03/15, Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:

|Has anyone any examplse of using the Enumerable module?  I've had a 
|look around, but not found much.

What kind of examples you have in mind?

Array class is a example.  Enumerable module add set of methods
depending on `each', among which are `collect', `detect', `sort',
etc. to a including class.

|  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.  I could create my own object for this, of course, but
|why re-invent unless I must?

Hmm, names for bits..., I'm afraid there's no direct class for that
purpose in Ruby.  But it's easy for define such class.

  class BitField
    def initialize(fields)
      @bits = 0
      @fields = fields
    end
    def set(bits)
      @bits = bits
    end
    def [](name)
      @bits[@fields[name]]
    end
  end

  if $0 == __FILE__
    bits = BitField::new(:Foo=>0, :Bar=>1)
    bits.set(0b01)
    p bits[:Foo]
    p bits[:Bar]
  end

Re-inventing wheel is fun using Ruby. :-)

							matz.

In This Thread