Sunday, March 8, 2009

Hard Stuff in Ruby- Part 5: Singletons

Okay. There is some weird stuff with this one.

The simplest application is that you can use it to overwrite class behavior on a specific object.

def instance_of_object.method
end

What else? It might look like this:
class << instance_of_object
def method
end
end


This has been described as an essential technique for metaprogramming, but the low hanging fruit is very basic. If you want an object that doesn't necessarily tow the line according to it's class, use one of these.



Hard stuff list:
-Metaprogramming
-Blocks
-Lambdas
-eval methods
-bindings
-Singleton
-Method Missing

No comments:

Post a Comment