def Monkey(word):
class _Monkey(object):
def shout(self):
print word
return _Monkey
class Chimp(Monkey("foo")):
pass
class Gorilla(Monkey("bar")):
pass
c = Chimp()
c.shout() # prints "foo"
g = Gorilla()
g.shout() # prints "bar"
In other words, the Ruby code you've provided is basically a class factory taking advantage of closures. The Python example shows that a bit more clearly.What I'm looking for is something equivalent to Python's "metaclass" declaration, which lets you completely control the way the class object is created. I've been told by fairly knowledgeable Rubyists that there isn't really an equivalent, and that after-the-fact monkeypatching or factory solutions like yours are the only way to come at the problem in Ruby.