From: Clemens Hintze Date: 1999-07-08T07:14:11+02:00 Subject: [ruby-talk:00434] Re: New feature for Ruby? --Boundary-=_ynEcdyGgxxpkLoRellNMPapqfWkH 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 writes: > [...] >You can replace the methods of existing class. For exapmle, > > class Integer > def succ(n=1) > ... > end > alias next succ > def pred(n=1) > ... > 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... 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-=_ynEcdyGgxxpkLoRellNMPapqfWkH Content-Type: text/x-Ruby; name="Interval.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="Interval.rb" #!/bin/env ruby def interval(start, stop, step=3D1) =09Interval::new(start, stop, step) end class Interval =09include Enumerable =09attr_reader :first, :last, :step =09def initialize(start, stop, step=3D1) =09=09raise ArgumentError, "XXX1" if not (start.respond_to?(:<=3D>)) =09=09up =3D ((start <=3D> stop) <=3D 0)=20 =09=09@cmp =3D up ? (:<=3D) : (:>=3D) =09=09@advfn =3D up ? (:succ) : (:pred) =09=09raise ArgumentError, "XXX2" if not (start.respond_to?(@advfn)) =09=09raise ArgumentError, "XXX3" if step <=3D 0 =09=09@first =3D start =09=09@last =3D stop =09=09@step =3D step =09end =09def each =09=09value =3D @first =09=09while (cmp =3D (value <=3D> @last)).send(@cmp, 0) =09=09=09yield value =09=09=09break if cmp =3D=3D 0 =09=09=09value =3D value.send(@advfn, @step) =09=09end =09=09nil =09end =09def [](index) =09=09i =3D 0 =09=09each do |element| =09=09=09return element if i =3D=3D index =09=09=09i +=3D 1 =09=09end =09=09nil =09end end if __FILE__ =3D=3D $0 =09# As I have learned now, that with that sample syntax, I can =09# override already defined methods, we can at once "improve" class =09# Fixnum, Float and String to show, how it would works together =09# with Interval =09class Fixnum =09=09def succ(n=3D1) =09=09=09self + n =09=09end =09=09def pred(n=3D1) =09=09=09self - n =09=09end =09end =09=09=09 =09class Float =09=09def succ(n=3D1) =09=09=09self + n =09=09end =09=09def pred(n=3D1) =09=09=09self - n =09=09end =09end =09class String =09=09alias oldsucc succ =09=09def succ(n=3D1) =09=09=09s =3D self =09=09=09for i in 1..n =09=09=09=09s =3D s.oldsucc =09=09=09end =09=09=09s =09=09end =09end =09# Here begin the examples. =09print "=3D=3D=3DUpwards Interval \"a\" .. \"e\"=3D=3D=3D\n" for i in interval("a", "e") =09=09p(i) =09end =09print "=3D=3D=3DDownwards Interval 9 .. 1=3D=3D=3D\n" =09for i in interval(9, 1) =09=09p(i) =09end =09print "=3D=3D=3DUpwards 1.0 .. 9.0 step 1.25=3D=3D=3D\n" =09for i in interval(1.0, 9.0, 1.25) =09=09p(i) =09end =09print "=3D=3D=3DDownwards 9 .. 1 step 3=3D=3D=3D\n" =09i =3D Interval::new(9, 1, 3) =09i.each{ |el| p(el) } =09print "=3D=3D=3DIs 3 in that interval? (answer: true): " =09p(i.include?(3)) =09print "=3D=3D=3DIs 2 in that interval? (answer: false): " =09p(i.include?(2)) =09print "=3D=3D=3DWhat is the first element? (answer: 9): "; p(i[0]) =09print "=3D=3D=3DWhat is the second element? (answer: 6): "; p(i[1]) =09print "=3D=3D=3DWhat is the third element? (answer: 3): "; p(i[2]) end --Boundary-=_ynEcdyGgxxpkLoRellNMPapqfWkH--