From: Michael Neumann Date: 2001-11-30T01:19:44+09:00 Subject: [ruby-talk:26925] Re: short article draft for review Tobias Reif wrote: > Hi, > > here is a draft for a very short article about Ruby. All in all I want > to leave it like it is, but if you find major errata/bugs/issues, please > post corrections/fixes. (or mail 'em offlist; it's probably not worth a > big thread) > > Easy to extend existing classes: > > class Numeric > def pos > if self < 0 > 0 > else > self > end > end > end > puts 4.pos > puts 0.pos > puts -4.pos It's a great feature but should be used with caution, because it has a global change. > Recursion, blocks as arguments: > > def upto limit,current=1,&block > yield current > if current < limit > upto limit,current+=1,&block > end > end Recursion is no special feature of Ruby. Almost every language has it. > ---------------------------------------- > > Objects: > > class Digits_to_english < String > def to_e > self.scan(/\d/).map do |digit| > case digit > when '0' then 'zero' > when '1' then 'one' > when '2' then 'two' > when '3' then 'three' > when '4' then 'four' > when '5' then 'five' > when '6' then 'six' > when '7' then 'seven' > when '8' then 'eight' > when '9' then 'nine' > end > end > end > end > > d2e = Digits_to_english.new '55793' > puts d2e > puts d2e.to_e > > # 55793 > # five > # five > # seven > # nine > # three I don't get the point here. Why do you call it "Objects". Wouldn't "Classes" as title be better? Or perhaps "Everything is an object", even classes ? For me, this is also very interesting: p (10 ** 1000) Singleton methods: a = "test" def a.output print self.to_s end a.output b = a b.output # OK b = "test" b.output # Error Mix-ins (instead of multiple inheritance): module B def test end end class A include B end A.new.test Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de