[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Gamont Gamont

11/12/2007 1:55:00 PM

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
--
Posted via http://www.ruby-....

9 Answers

Morton Goldberg

11/12/2007 6:37:00 PM

0

On Nov 12, 2007, at 8:54 AM, 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.

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

7stud --

11/12/2007 7:21:00 PM

0

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

--
Posted via http://www.ruby-....

Todd Benson

11/12/2007 7:37:00 PM

0

On Nov 12, 2007 1:21 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> 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

7stud --

11/12/2007 7:50:00 PM

0

Todd Benson wrote:
>
> c.f == C.f
>

nil == nil

So what?
--
Posted via http://www.ruby-....

Todd Benson

11/12/2007 8:31:00 PM

0

On Nov 12, 2007 1:50 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Todd Benson 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

Codeblogger

11/12/2007 8:47:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

> Is the NilClass object always 4?


Yes, it is.

Rob Biedenharn

11/12/2007 8:56:00 PM

0

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


Robert Dober

11/12/2007 9:00:00 PM

0

On Nov 12, 2007 9:46 PM, Codeblogger <codeblogger@gmail.com> 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 ;)

R.
>



--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Todd Benson

11/13/2007 2:38:00 AM

0

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 ;)
>
> 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