Hi all,
I have begun to learn, how to extend Ruby using C. Doing that, I think
I can do some "real" work while learning.
As it has disturbes me several times, that a Range is only able to
iterate upwards but not downwards, I have chosen to add some features
to class Range and others, to let Range behave more like Smalltalk's
Intervall class.
So here are my ideas:
I would propose, that the class Range should behave more like the Interval
class of Smalltalk. That means it should be possible to do the
following:
- Range::new(1, 9)
- Range::new(1, 9, 2)
- Range::new(1, 9, -2) -- ERROR!
- Range::new(9, 1)
- Range::new(9, 1, 2)
- Range::new(9, 1, -2) -- ERROR!
- Range::new("a", "z", 3)
- Range::new(1.2, 2.9, 0.01)
As you can see, I would like to express the stepsize only via an
absolute value. The direction should only be given by "beg < end" or "beg
> end". I would raise an exception if the stepsize would be negativ, as
the general syntax should be:
Range::new(beg, end, step=1)
That means the beginning ever comes first. If here I had a negative
stepsize, I would, probably, never reach my end.
The call "Range::new(1, 9)" or "Range::new(9, 1)" could also be
written as "1..9" and "9..1" (perhaps we could have a global function
"range" (like Python) which would instantiate Ranges via Range::new?).
Every class which wants to be used as Range, should implement a
"succ", "pred" and a "compare method" to enable us to detect the end
of iteration.
The "succ" and the "pred" method should be able to get an optional
argument describing the increment/decrement size. Whereas 5.succ(-2)
would be the same as 5.pred(2)! And vice versa.
Furthermore it would be nice to have a read-access function "[]", so
that the following would be possible:
r = "a".."z" # or Range::new("a", "z")
print r[3] # would print 'c'!
The special case would be the String class, as here the "succ" and the
"pred" functions would do a magic addition like in Perl. That means:
"z".succ # would deliver "aa"
"aa".pred # would deliver "z"
Please let me know your opinion! Is that all silly, or would you also
like such features?
Bye,
Cle.
--
this message is re-posted by matz the list admin.