From: Gavin Sinclair Date: 2004-10-06T10:36:43+09:00 Subject: Re: A ruby course On Wednesday, October 6, 2004, 3:24:12 AM, Brian wrote: > Hello Group, > I need more help. > Gavin Sinclair wrote: >> >> p51. Good exercise. How about another? >> > Anybody has an idea for an exercise on extending existing classes. So > far the only thing I have come up with is: > \exercise{Fibonacci II}{ > Extend \codetype{Integer} with a function fib that calculates the > corresponding fibonacci number.} > And my creativity is at an all time low. There are plenty of examples at http://extensions.rubyforge.org. I was thinking of something like String#indent as an exercise, but you haven't taught regexen at this point, so that's out. But something else might do. >> p52. It would be good to teach extending objects, because it's core to >> Ruby, and important when understanding "class methods". But you >> can emphasise that it's advanced material, so students needn't be >> worried if they don't get it straight away. (It's not *that* >> hard to accept, anyway... :) >> > Anybody nows of a good example where it is neccessary to extend an > existing object? > I have never used this feature and I don't know when to use it in a > sensible way. It could be something academic like changing the to_s output for a particular array/string. But really, the main point of this feature (adding methods to objects) is to add methods to classes (which are objects). So perhaps use that as an example. >> p55. Missing exercises. > Somebody has an idea for an exercise on using modules (as namespace and > mixin?) A favourite of mine is building a new class, then mixing in Enumerable and Comparable. So you're not building a new module, but reusing two existing ones. Gavin