[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02274] Re: Adding method to Object -- Why doesn't this work?

From: h.fulton@...
Date: 2000-03-30 19:29:46 UTC
List: ruby-talk #2274
Thanks much, Dave!

I'm a little in the dark about singleton methods. I'm 
not sure when one would use one instead of an instance
method.

BTW< That must be some kind of record for a speedy 
response. If you had been any faster, I would have 
received the reply before I asked the question. Are 
you using a time machine?

I had actually thought it might be hours till I got
an answer... most of the people on this list are asleep
right now, aren't they? 

Hal

> h.fulton@att.net writes:
> 
> > Greetings, fellow Ruby enthusiasts.
> > 
> > Perhaps I have a fundamental misunderstanding here.
> 
> Perhaps.. ;-)
> 
> When you defined Object.foobar, you're creating a singleton method in
> Object (one that should be called as Object.foobar), not an instance
> method. Thus the method is not available through a message to self.
> 
> So, you could rewrite it as an instance method, and all's fine. (I
> also used the wonderful 'defined?' expression to check for foorbar in
> a superclass.
> 
> class Object
>   def foobar
>     print "I am the ultimate foobar!\n"
>   end
> end
> 
> class Foo
>   def foobar
>     print "I am Foo::foobar... "
>     if defined? super
>       print "my parent has a foobar!\n"
>       super
>     else
>       print "my parent does NOT have a foobar.\n"
>     end
>     print "My parent is ", self.type.superclass, "\n"
>   end
> end
> 
> Foo.new.foobar
> 
> -- 
> Thomas Consulting.
> Innovative and successful developments with Unix, Java, C, and C++. 
> 
> Now in bookstores:
>  The Pragmatic Programmer.  www.pragmaticprogrammer.com/ppbook/

In This Thread

Prev Next