This post originated from an RSS feed registered with Java Buzz
by Brian McCallister.
Original Post: (Ruby) Corrupted by JavaScript
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
I have been corrupted by JavaScript. Ruby really annoyed me when I
could not just add properties and functions totally willy-nilly to
instances. Ruby makes it easy to reopen classes, or even instances,
but it is not so obvious how to do so without creating a new scope
(and hence losing your closure).
The ~standared mechanism is to do something vicious like this:
Aside from the fact that this is a vicious hack, it relies on the
fact that send lets you invoke private methods
(define_method) on the singleton class (more recently
going by the alias of eigenclass) of foo. This is pretty
much a bug and a violation of the object. It's like object rape, or
something.
A nicer way to do it, to my mind, is to use anonymous modules. The
really nice part of using anonymous modules is that the
Module constructor takes a block which is evaluated in
the context of the new module, letting you legally call
private methods, like define_method.