From: Robert Klemme Date: 2005-09-29T04:06:44+09:00 Subject: Re: Python to Ruby: Two puzzlements... Elf M. Sternberg wrote: > I'm afraid that I'm coming from Python, a B&D language where I'm used > to everything be spelled out cleanly, and although I programmed in > Perl for many years that was also many years ago. Ruby is baffling > the heck out of me. > > 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 defined in Class: >> Class.methods.grep /ext/ => ["extend"] > It's just hanging there in space. Well, if you want to call it like that... Then String and Fixnum are also hanging in space. In fact these are very basic parts of Ruby that are essentially defined in the interpreter (in C). (Btw, you can nevertheless override most of the basic stuff which makes a great part of Ruby's flexibility!) # silly example: >> s="foo" => "foo" >> s.length => 3 >> class String >> alias __length length >> def length() 2 * __length end >> end => nil >> s.length => 6 > In the same > breath, what does the 'super' keyword do in the append_features() > method, "super" without brackets invokes the same method with the same arguments in the super class. You can also explicitely hand over arguments (or no arguments) by using brackets: >> class Foo >> def bar(x) puts "Foo: #{x}" end >> end => nil >> class Bar < Foo >> def bar(a,b) >> super(a) >> puts "Bar: #{b}" >> end >> end => nil >> Bar.new.bar 1, 2 Foo: 1 Bar: 2 => nil > and why does it need a 'self' in front of it? Inside the class "self" references the class itself: >> class Foo >> p self >> end Foo => nil Whenever you see "def .(...)... end" then a singleton method is defined for this instance. Only this instance has this method. If the instance happens to be a class you get roughly the same as a class method in another language. > Second, I've seen the following constructions: > > validates :url :with => %r{^http:.+\.(gif|jpg|png)$}i > > and > > check_associations %w(friends downloads links) > > I'm guessing that the "%r" and "%w" are the ruby equivalents of perl's > qr{} and qw{} constructs Right! > (does this mean there's a %qq() and %qx() as > well?), >> %x{date} => "Wed Sep 28 20:56:11 2005\n" single quotes >> %q{aa: #{1+2}} => "aa: \#{1+2}" double quotes >> %Q{aa: #{1+2}} => "aa: 3" Can't remember whether these are the same in Perl. Note that #{} evaluates the expression, converts it into a string via #to_s and includes it at this place; this is similar to sprintf("%s", expr). > but I can't find documentation to that effect inside the > references available on-line. http://www.ruby-doc.org/docs/ProgrammingRuby/html/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! :-)) It's really worth every penny. Kind regards robert