Method inheritance? 1
A couple of days ago I was poking around Park Place source code and I saw something resembling the following syntax:
# ... is some arbitrary string in this case
class A < B '...'
# ...
endI am no Ruby expert, in fact, I have written very little Ruby code so far. However, the code above looked really odd to me. It looks like class A is inheriting from class B, and class B is taking some arbitrary string as an argument.
Well, after closer inspection I was completely wrong on what B is. B is not a class at all, in fact, it is a method under cover.
def B str
# do something with str variable here
end'...' is some argument to the B method, which makes a lot of sense, but how can class A inherit from a method? If you try the code above, whatever is in method B will execute, but you will get an error when class A is trying to inherit from B. The reason is simple, class A is expecting to inherit from a class not a method. To prevent the error, you need to somehow construct a class in method B.
def B str
# do something with str variable here
Class.new
endRuby automatically returns the last line as the return value. In this case, you will not get the error anymore.
The first question one should ask is, why would anyone want to do this? The code I showed above is a very simple version of the real code that appeared in Park Place. In Park Place it creates a new class and adds methods to the class based on the input. This is powerful because it allows one to hide the base class since it does not actually exist. On top of that, using this technique one can write code that writes code. Ruby on Rails uses similar techniques to achieve all the magic.

Camping uses it for its route classes -- when the R method is called, it registers an anonymous class with some special methods (used to associate the string with the class) and puts it in a global routing table.
class Index < R '/' end
Creates a superclass for Index (which then catches the subclassing operation and ties it all together), and associates it with the URL "/"