tags:

views:

537

answers:

2
class A
  def a
    puts 'in #a'
  end
end

class B < A
  def a
    b()
  end
  def b
    # here i want to call A#a.
  end
end  
+2  A: 

There's no nice way to do it, but you can do A.instance_method(:a).bind(self).call, which will work, but is ugly.

You could even define your own method in Object to act like super in java:

class SuperProxy
  def initialize(obj)
    @obj = obj
  end

  def method_missing(meth, *args, &blk)
    @obj.class.superclass.instance_method(meth).bind(@obj).call(*args, &blk)
  end
end

class Object
  private
  def sup
    SuperProxy.new(self)
  end
end

class A
  def a
    puts "In A#a"
  end
end

class B<A
  def a
  end

  def b
    sup.a
  end
end
B.new.b # Prints in A#a
sepp2k
complicated solution for a fairly simple task.
klochner
@klochner I disagree, this solution was exactly what I needed... reason: I wanted to generically call super method of a different method, but without the need to alias every single one that I wanted to be able to do this for, so a generic way to invoke super is pretty useful
Mike Stone
+7  A: 
class B < A

  alias :super_a :a

  def a
    b()
  end
  def b
    super_a()
  end
end
Beffa