Class <<Time?

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.

How this works ?

thanks

On Nov 12, 2007, at 8:54 AM, Gamont Gamont wrote:

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’.

Regards, Morton

Gamont Gamont wrote:

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.

How this works ?

class Dog
def Dog.bark
puts ‘Woof’
end

def self.speak
puts ‘I am a Dog.’
end

class <<Dog
def run
puts “Run run”
end
end

class <<self
def growl
puts “Grrrrr”
end
end

end

class <<Dog
def sayhi
puts ‘hi’
end
end

Dog.bark
Dog.speak
Dog.run
Dog.growl
Dog.sayhi

–output:–
Woof
I am a Dog.
Run run
Grrrrr
hi

On Nov 12, 2007 1:21 PM, 7stud – [email protected] wrote:

def run

end
Dog.run
Dog.growl
Dog.sayhi

–output:–
Woof
I am a Dog.
Run run
Grrrrr

class C
class << self
def f
puts “hi”
end
end
def f
puts “bye”
end
end

c = C.new
c.f
C.f
c.f == C.f

Todd

Todd B. wrote:

c.f == C.f

nil == nil

So what?

Is the NilClass object always 4?

Yes, it is.

On Nov 12, 2007, at 3:46 PM, Codeblogger wrote:

Is the NilClass object always 4?

Yes, it is.

…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.

-Rob

On Nov 12, 2007 9:46 PM, Codeblogger [email protected] wrote:

Is the NilClass object always 4?

Yes, it is.
Now it is in MRI and JRuby, but it definetly does not need to be :wink:

R.

On Nov 12, 2007 1:50 PM, 7stud – [email protected] wrote:

Todd B. wrote:

c.f == C.f

nil == nil

So what?

You’re right. What seemed strange to me is that c.f.object_id and
C.f.object_id were both 4. Is the NilClass object always 4?

Todd

On Nov 12, 2007 2:59 PM, Robert D. [email protected] wrote:

Yes, it is.
Now it is in MRI and JRuby, but it definetly does not need to be :wink:

R.

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