How to get the return value from method of the super class?

Discussion in 'Ruby' started by Josef Wolf, Sep 9, 2009.

  1. Josef Wolf

    Josef Wolf Guest

    Hello,

    I am trying to override a method of a superclass within my derived class.
    To do the real work, I need to call the original method of the superclass.
    But when I try to get the return value of the super method, I only get
    the object, not the return values I expected. Here's a code snippet to
    demonstrate the problem, stripped from everything not related to the
    problem:

    jw@raven> cat ./test.rb
    #! /usr/bin/ruby

    require 'tk'
    require 'pp'

    root = TkRoot.new { title "pcb.rb" }

    class TkDerivedCanvas < TkCanvas
    def coords(tag, *args)
    super(tag, args)
    end
    end

    orig = TkCanvas.new:)scrollregion=>[0,0,500,400]).pack()
    derived = TkDerivedCanvas.new:)scrollregion=>[0,0,500,400]).pack()

    origrect = TkcRectangle.new(orig, [100,100], [300, 200], :fill=>"red")
    derivedrect = TkcRectangle.new(derived, [100,100], [300, 200], :fill=>"red")

    pp orig.coords(origrect)
    pp derived.coords(derivedrect)

    jw@raven> ./test.rb
    [100.0, 100.0, 300.0, 200.0]
    #<TkDerivedCanvas:0xb7cce11c @path=".w00001">
    jw@raven>


    Any ideas what I am doing wrong here?
    Josef Wolf, Sep 9, 2009
    #1
    1. Advertising

  2. Hi --

    On Wed, 9 Sep 2009, Josef Wolf wrote:

    > Hello,
    >
    > I am trying to override a method of a superclass within my derived class.
    > To do the real work, I need to call the original method of the superclass.
    > But when I try to get the return value of the super method, I only get
    > the object, not the return values I expected. Here's a code snippet to
    > demonstrate the problem, stripped from everything not related to the
    > problem:
    >
    > jw@raven> cat ./test.rb
    > #! /usr/bin/ruby
    >
    > require 'tk'
    > require 'pp'
    >
    > root = TkRoot.new { title "pcb.rb" }
    >
    > class TkDerivedCanvas < TkCanvas
    > def coords(tag, *args)
    > super(tag, args)
    > end
    > end
    >
    > orig = TkCanvas.new:)scrollregion=>[0,0,500,400]).pack()
    > derived = TkDerivedCanvas.new:)scrollregion=>[0,0,500,400]).pack()
    >
    > origrect = TkcRectangle.new(orig, [100,100], [300, 200], :fill=>"red")
    > derivedrect = TkcRectangle.new(derived, [100,100], [300, 200], :fill=>"red")
    >
    > pp orig.coords(origrect)
    > pp derived.coords(derivedrect)
    >
    > jw@raven> ./test.rb
    > [100.0, 100.0, 300.0, 200.0]
    > #<TkDerivedCanvas:0xb7cce11c @path=".w00001">
    > jw@raven>
    >
    >
    > Any ideas what I am doing wrong here?


    Yes; the syntax of your call to super is wrong. If you want to call
    super with the same arguments as the ones you got, just do:

    super

    with no argument list. Also, in general, if you want to call another
    method with the same arguments, you need to do:

    def m(*args)
    other(*args) # note the *
    end

    If you leave the * off of the call to other, you'll be sending a
    single argument (an array) instead of a list of arguments (the
    elements of the array).

    In the case of super you don't need to do that; just don't provide any
    argument list at all. (That's because super is a keyword, not a
    method, and has its own semantics.)


    David

    --
    David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
    Ruby/Rails training, mentoring, consulting, code-review
    Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2)

    September Ruby training in NJ has been POSTPONED. Details to follow.
    David A. Black, Sep 9, 2009
    #2
    1. Advertising

  3. Josef Wolf

    Josef Wolf Guest

    On Wed, Sep 09, 2009 at 08:17:05PM +0900, David A. Black wrote:
    [ ... ]
    >> class TkDerivedCanvas < TkCanvas
    >> def coords(tag, *args)
    >> super(tag, args)
    >> end
    >> end

    > [ ... ]
    > with no argument list. Also, in general, if you want to call another
    > method with the same arguments, you need to do:
    >
    > def m(*args)
    > other(*args) # note the *
    > end
    >
    > If you leave the * off of the call to other, you'll be sending a
    > single argument (an array) instead of a list of arguments (the
    > elements of the array).


    Thanks for the explanation, David! Works better now.
    Josef Wolf, Sep 9, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,758
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    719
    Bob Willan
    Nov 22, 2003
  3. zweibieren

    A need for super.super.method()

    zweibieren, May 7, 2009, in forum: Java
    Replies:
    0
    Views:
    448
    zweibieren
    May 7, 2009
  4. fi3nd
    Replies:
    0
    Views:
    382
    fi3nd
    Apr 8, 2010
  5. Matthew Braid

    $class::SUPER as well as $instance::SUPER?

    Matthew Braid, Aug 6, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    307
    Matthew Braid
    Aug 9, 2004
Loading...

Share This Page