From: Gavin Sinclair Date: 2004-10-06T10:51:39+09:00 Subject: Re: A ruby course On Wednesday, October 6, 2004, 5:18:17 AM, Brian 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? >> >> My 2 cents. >> Ed >> > I'm sorry, but this was exactly the example I gave. It seems we have a > similar mindset ;) Maybe extend this, then: module InlineMath def fib; ...; end def fac; ...; end def sin; ...; end def cos; ...; end def tan; ...; end end class Numeric include InlineMath end Students have to implement those methods, and try it out with real numbers. Caveat: what about 3.5.fib and 3.5.fac? Do the trigonometric methods assume degress or radians? Gavin