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.