How to get a super entity?

Suppose you are rewriting a method into a subclass with a different reality:

class A def foo(arg) # arity is 1 # doing something here end end class B < A def foo(arg1, arg2) # arity is 2 super(arg1) # <- HERE end end 

Is there any way to get arity super in line HERE?

(Real use case: I call super , knowing that the superclass does not accept any arguments. However, if the implementation of the superclass (in gem) ever changes, I would like to issue a warning.)

Thank you for your help!

+7
source share
1 answer

As for your real use case: there is no need to check the arguments yourself. Just call

 super(arg1) 

and Ruby will raise an ArgumentError if the argument count does not match.

Update

Due to some downvotes, I think I should answer your original question.

How to get the super attribute?

Starting with Ruby 2.2, Method#super_method and UnboundMethod#super_method :

 class A def foo(arg) end end class B < A def foo(arg1, arg2) end end B.instance_method(:foo).arity #=> 2 B.instance_method(:foo).super_method.arity #=> 1 

Inside B#foo you can write:

 class B < A def foo(arg1, arg2) method(__method__).super_method.arity #=> 1 end end 
+5
source

All Articles