--Boundary- nEcdyGgxxpkLoRellNMPapqfWkH Content-Type: text/plain Content-Transfer-Encoding: 8bit On Thu, 08 Jul 1999, you wrote: >Hi, > >This is not a comment for your code, sorry. I will check it later. > >In message "[ruby-talk:00431] Re: New feature for Ruby?" > on 99/07/07, Clemens Hintze <c.hintze / gmx.net> writes: > [...] >You can replace the methods of existing class. For exapmle, > > class Integer > def succ(n > ... > end > alias next succ > def pred(n > ... > end > alias prev pred > end > >will do the job for you. Oh, how dynamic Ruby is. :-) :-00000 Woah! T h a t i s w o n d e r f u l !!! :-))))))))))))))))) I didn't know, that this is possible... I did know concerning Singleton methods... but this... <cannot believe what he sees...> But does that mean, I cannot redefine an already defined class? Means, if I want to totaly redefine the class e.g. Regexp. Perhaps use another regexp engine, so nothing from the old class should remain! As I have learned now, it is not enough to simply write class Regexp; ...; end That would only enhanve/modify existant methods of already existant class Regexp, true? What I would have to do in that case? Not that I plan to do so. Only I want to know :-) BTW: I have attached Interval.rb again to that mail. This time with classes Fixnum, Float and String modificated. Now the example looks like it should. Only I would not define a String#pred in Ruby now. Too lazy, sorry! :-) Thanks again for that hint... and, of course, for that feature... and, last but not least, for Ruby! :-))))) > > matz. \cle PS: Would it makes sense to introduce that feature in the Ruby-Manual? Perhaps it is, but I cannot remember I did see that! Furthermore please explain `alias' more a little bit. I could not see from the manual, whether the syntax is `alias old new' or `alias new old'. --Boundary- nEcdyGgxxpkLoRellNMPapqfWkH Content-Type: text/x-Ruby; name nterval.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="Interval.rb" #!/bin/env ruby def interval(start, stop, step=1) Interval::new(start, stop, step) end class Interval include Enumerable attr_reader :first, :last, :step def initialize(start, stop, step=1) raise ArgumentError, "XXX1" if not (start.respond_to?(:<=>)) up = ((start <=> stop) <= 0) @cmp = up ? (:<=) : (:>=) @advfn = up ? (:succ) : (:pred) raise ArgumentError, "XXX2" if not (start.respond_to?(@advfn)) raise ArgumentError, "XXX3" if step <= 0 @first = start @last = stop @step = step end def each value = @first while (cmp = (value <=> @last)).send(@cmp, 0) yield value break if cmp == 0 value = value.send(@advfn, @step) end nil end def [](index) i = 0 each do |element| return element if i == index i += 1 end nil end end if __FILE__ == $0 # As I have learned now, that with that sample syntax, I can # override already defined methods, we can at once "improve" class # Fixnum, Float and String to show, how it would works together # with Interval class Fixnum def succ(n=1) self + n end def pred(n=1) self - n end end class Float def succ(n=1) self + n end def pred(n=1) self - n end end class String alias oldsucc succ def succ(n=1) s = self for i in 1..n s = s.oldsucc end s end end # Here begin the examples. print "===Upwards Interval \"a\" .. \"e\"===\n" for i in interval("a", "e") p(i) end print "===Downwards Interval 9 .. 1===\n" for i in interval(9, 1) p(i) end print "===Upwards 1.0 .. 9.0 step 1.25===\n" for i in interval(1.0, 9.0, 1.25) p(i) end print "===Downwards 9 .. 1 step 3===\n" i = Interval::new(9, 1, 3) i.each{ |el| p(el) } print "===Is 3 in that interval? (answer: true): " p(i.include?(3)) print "===Is 2 in that interval? (answer: false): " p(i.include?(2)) print "===What is the first element? (answer: 9): "; p(i[0]) print "===What is the second element? (answer: 6): "; p(i[1]) print "===What is the third element? (answer: 3): "; p(i[2]) end --Boundary- nEcdyGgxxpkLoRellNMPapqfWkH--