From: Edgardo Hames Date: 2004-10-06T04:25:25+09:00 Subject: Re: A ruby course On Wed, 6 Oct 2004 04:18:17 +0900, Brian Schr�der wrote: > Edgardo Hames wrote: > > On Wed, 6 Oct 2004 02:24:12 +0900, Brian Schr�der > > wrote: > > > >>Anybody has an idea for an exercise on extending existing classes. So > >>far the only thing I have come up with is: > >> > > > > > > Taken from the Design Patterns book, Decorator pattern: > > > > "Attach additional responsibilities to an object dynamically. > > Decorators provide a flexible alternative to subclassing for extending > > functionality." > > > > You could extend Integer with a fibonacci method, so that you can > > write something like: > > > > puts 0.fibonacci > 0 > > puts 1.fibonacci > 1 > > puts 5.fibonacci > 5 > > Should be 12, shouldn't it? 0.fib > 0 1.fib > 1 2.fib > 1 3.fib > 2 4.fib > 3 5.fib > 5 > I'm sorry, but this was exactly the example I gave. It seems we have a > similar mindset ;) I'm really sorry, I must admit I didn't really paid much attention to the example. I'm replying messages while in a training session... :( But anyway, you can think of the Visitor or Decorator pattern, for extending classes. Did I mention adding a fib method to Fixnum? ;) Regards, Ed -- " Don't relax! It's only your tension that's holding you together."