Calling super from some other method

Say I have:

class A
  def method
    puts "A#method"
  end
end

class B < A
  def method
    other_method
  end
  def other_method
    method     # <- want A#method here
  end
end

In B#other_method, I want to call A#method.

Within B#method, I can call A#method with super. But I can’t figure
out how to do the same within B#other_method. In C++, I’d just
statically qualify the class name in the source (A::method), but I
don’t know how to do this in Ruby.

Hi –

Say I have:

class A
  def method
    puts "A#method"
  end
end

class B < A
  def method
    other_method
  end
  def other_method
    method     # <- want A#method here
  end
end

In B#other_method, I want to call A#method.

Within B#method, I can call A#method with super. But I can’t figure
out how to do the same within B#other_method. In C++, I’d just
statically qualify the class name in the source (A::method), but I
don’t know how to do this in Ruby.

Hmmm… This sounds like one of the things in the “Mommy, where do
methods come from?” thread. If I’m right about that, here’s a
possibly relevant exchange between Chris Pine and Nobu:

Chris:

Finally (and I’m sure I should know this one already), how would I
call Object#to_s on foo if I have already redefined to_s? (Is this
even possible?)

Nobu:

Impossible outside redefined to_s, unless it’s aliased.

I think the general principle is that you can’t just call A#method, in
the abstract; you have to send the message to a specific object, and
since you’ve redefined the method, the object in question does
something else when you send it that message.

David

···

On Mon, 23 Dec 2002, Matt Armstrong wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

dblack@candle.superlink.net writes:

Within B#method, I can call A#method with super. But I can’t
figure out how to do the same within B#other_method. In C++, I’d
just statically qualify the class name in the source (A::method),
but I don’t know how to do this in Ruby.

Hmmm… This sounds like one of the things in the “Mommy, where do
methods come from?” thread. If I’m right about that, here’s a
possibly relevant exchange between Chris Pine and Nobu:

Chris:

Finally (and I’m sure I should know this one already), how would I
call Object#to_s on foo if I have already redefined to_s? (Is
this even possible?)

Nobu:

Impossible outside redefined to_s, unless it’s aliased.

I think the general principle is that you can’t just call A#method,
in the abstract; you have to send the message to a specific object,
and since you’ve redefined the method, the object in question does
something else when you send it that message.

Thanks, I figured as much.

With the notable exception of ‘super’, which seems to be a special
magic present because calling the superclass’ version of a method is
sometimes convenient. I haven’t thought of a good design reason to
provide this only from the method of the same name in the subclass.

I ran into the limitation of ‘super’ when refactoring. I had
redefined a method in a subclass but the subclass’ implementation was
quite long. I wanted to break it up into a few different methods, but
couldn’t because it called super() in the middle of the logic.

I played games with alias to get around it, so no big deal. It just
didn’t seem as natural and easy as what I’ve come to expect from
Ruby. :wink: