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
views:
537answers:
2
+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
2009-08-09 11:23:26
complicated solution for a fairly simple task.
klochner
2009-08-10 06:33:54
@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
2010-09-27 19:32:29