From: "Florian Groß" Date: 2005-09-29T04:09:08+09:00 Subject: Re: Python to Ruby: Two puzzlements... Elf M. Sternberg wrote: > I've been looking at two examples, and I was wondering if someone could > explain to me what the Hell is going on. The first is from > http://redhanded.hobix.com/bits/hyperextended.html, and my question is > about the 'extend' keyword there. Where does that came from? In what > object is it defined? It's Object#extend and ri knows about it: > ---------------------------------------------------------- Object#extend > obj.extend(module, ...) => obj > ------------------------------------------------------------------------ > Adds to _obj_ the instance methods from each module given as a > parameter. > > module Mod > def hello > "Hello from Mod.\n" > end > end > > class Klass > def hello > "Hello from Klass.\n" > end > end > > k = Klass.new > k.hello #=> "Hello from Klass.\n" > k.extend(Mod) #=> # > k.hello #=> "Hello from Mod.\n" > what does the 'super' keyword do in the append_features() > method, and why does it need a 'self' in front of it? I've tried > reading ruby-docs without finding much illumination. def obj.foo defines a method on an object itself. If you do def self.method in a module you define a method on the module object itself and not as an instance method of the module. super calls the implementation of the method in a super class -- in the sample it calls Module#append_features and ri knows about it: > ------------------------------------------------- Module#append_features > append_features(mod) => mod > ------------------------------------------------------------------------ > When this module is included in another, Ruby calls > +append_features+ in this module, passing it the receiving module > in _mod_. Ruby's default implementation is to add the constants, > methods, and module variables of this module to _mod_ if this > module has not already been added to _mod_ or one of its ancestors. > See also +Module#include+. > I'm guessing that the "%r" and "%w" are the ruby equivalents of perl's > qr{} and qw{} constructs (does this mean there's a %qq() and %qx() as > well?), but I can't find documentation to that effect inside the > references available on-line. Yup, and the Pickaxe knows about them: http://www.rubycentral.com/book/language.html#UB > (And before someone says "Buy the > Pickaxe!" I'll just say that I spent my book budget this month on the > Rails book and any other purchases will have to wait until my next > paycheck.) Buy the Pickaxe! Or read the old one online. ;)