[#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:02273] Re: Adding method to Object -- Why doesn't this work?

From: Dave Thomas <Dave@...>
Date: 2000-03-30 18:40:56 UTC
List: ruby-talk #2273
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