From: Markus Date: 2004-10-06T03:12:35+09:00 Subject: Re: A ruby course On Tue, 2004-10-05 at 10:24, Brian Schræ—¦der wrote: > Hello Group, > > I need more help. > > 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. * Sub class (or extend) array to support "card deck" semantics, e.g. cut, shuffle, deal, and (*smile*) maybe even stack? * Add set-like operations to hash (e.g. &&, ||) * Here's one I find this very useful: add aggregate behaviour to hashes. For example have { :a=>3, :b=>[0,5,2], :c=>8 } + {:b=>[7], :c=>4} return { :a=>3, :b=>[0,5,2,7], :c=>12 } * Add some sort of simple substitution cypher to String > > 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? As far as I'm concerned, things like: def Symbol.intern self end but I don't think that's quite what your asking. > I have never used this feature and I don't know when to use it in a > sensible way. If you're talking about extending individual objects, the only time I can recall using it extensively was in a little text based adventure game. It's kind of odd now that I think about it: I love the ability but I use it very sparingly. > > p55. Missing exercises. > > Somebody has an idea for an exercise on using modules (as namespace and > mixin?) A linked list or tree package is the semi-standard answer. You can add it to any class & do "classical" lists & trees. You may want to note that this isn't very rubyesque thought. Maybe a simple version history "memento" setup? Or taint tagging with more history/details than tainted or not? Or some sort of math package (& use this to introduce Math)? -- Markus