Im see this example and i didnt understand how it works.
I dont find doc abou this construct.
I find about object-specific class (class <<a), but here
its a class.
class <<Time
alias old_now now
def now
old_now.to_f * FACTOR
end
end
and when this statement executes
Time.now, it calls the method in anonymous class and
no more in Time class.
No, this code doesn't change where the 'now' method resides. Time.now always calls the 'now' method in its singleton class.
How this works ?
It works because Time is an object as well as a class. So Time can (and does) have a singleton class (what you call an object-specific class) just as any other object can. The singleton class of a class is where class methods are defined, so the code you show simply overrides the Time class method 'now'.
...but only as a trivia answer as it relates to the implementation details of MRI -- the Matz Ruby Interpretter (aka, CRuby). It would be quite bad form to rely on this "fact" for any code that you expected to be portable.
I find it a little bit amusing that 4 is considered unlucky in
Japanese language, because of its implication of death - "shi". 9 is
unlucky too for different reasons.
Elsewise; obviously, no design should rely upon object ids. I was
just curious. On top of that, I like the fact that in irb, the code
that I posted previously yields a visual contradiction. You can make
it more obvious by having the puts methods output "yes" and "no".
Todd
···
On Nov 12, 2007 2:59 PM, Robert Dober <robert.dober@gmail.com> wrote:
> Yes, it is.
Now it is in MRI and JRuby, but it definetly does not need to be