class <<Time ?

G

Gamont Gamont

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
 
M

Morton Goldberg

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

Regards, Morton
 
7

7stud --

Gamont said:
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
 
T

Todd Benson

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

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
 
T

Todd Benson

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
 
R

Rob Biedenharn

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
 
T

Todd Benson

Now it is in MRI and JRuby, but it definetly does not need to be ;)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,352
Members
48,037
Latest member
BettinaArn

Latest Threads

Top