I still don't entirely understand the syntax for accessing the singleton class, but I have found a use for it. :) The goal is to be able to make an object include a mixin on the fly.
If you do self.class.send(:include,module_name) instead of using the singleton class then the change affects every instance, not just the one you intended.
So it seems to work pretty well. Ruby is all like "sure buddy, have some rope, take as much as you need". :)
I should say that this technique was designed to be a quick fix to let us work around some legacy code that really should be refactored and fixed properly. I'm not saying this is a sensible design pattern, but it is cool that ruby can do it.
Here is a good explanation of singleton classes in ruby.
No comments:
Post a Comment