Florian Growrote: > 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) #=> >> #<Klass:0x401b3bc8> 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. ;) Elf, It looks like you're getting the typical onslaught of friendly and useful advice. Nice, huh? If there's one thing that I might be able to add (unless someone else has pointed it out already), it's that you should have something called irb thrown in with your ruby installation. Between ri and irb you can do a lot of tinkering around with ruby concepts in isolation. In fact, irb works in a very similar way to the way that the python interpreter works when invoked alone on the commend line (except that when you ask it to exit, it does, instead of dropping that silly "Use Ctrl-Z plus Return to exit." line on you.). Basically, you can fire off any ruby that you could run in a script, and so you can see firsthand the effects of any language constructs whose purpose might not be clear. If you want to start tinkering with larger chunks of code, search this mailing list over the past week or so: there's been a discussion about whether the RDE tool from sakazuki-san ( http://homepage2.nifty.com/sakazuki/rde_e.html ) is more convenient that the code-running capabilites of SciTe (also packaged with the ruby installation, on windows anyway). Have fun, and screw around. I've learned most of my best ruby-fu by accident... mattD