Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the arity of "super"?

Tags:

ruby

Say you are overwriting a method in a subclass with a different arity:

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 a way to get the arity of super on line HERE?

(The real use case: I'm calling super knowing that the superclass doesn't take any arguments. However, if the superclass implementation (in a gem) ever changes, I'd like to issue a warning.)

Thanks for your help!

like image 496
svoop Avatar asked Oct 22 '12 11:10

svoop


People also ask

How do I increase the rarity of a sword?

Additionally, the rarity of a sword can be improved by using the Ascender, or by increasing the luck in the Appraiser settings . " Boss sniping " is used by players to get Unique- or rarer swords, this is done by using the (now defunct) rare bosses spawns channel or joining servers that are close to 10 hours in age.

Where can I find super spirit detergent in stray?

One of the early collectibles you'll need to find in Stray is Super Spirit Detergent, located in the Slums near the beginning of your adorable kitty journey. The merchant Azooz will sell you an Electric Cable you need to complete the fixing the Broken Tracker quest, but only if you find the detergent.

How many super rares are in each booster pack?

Every core booster pack's guaranteed to contain at least a Super Rare, in addition to eight Commons. Modern Super Rares have holofoil artwork, Level, and Attribute icons. Older Super Rares will only have holofoil artwork.


1 Answers

Regarding your real use case: there's no need to check the arguments yourself. Just call

super(arg1)

and Ruby will raise an ArgumentError if the argument count doesn't match.

Update

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

How to get the arity of “super”?

Starting with Ruby 2.2, there's 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

From within B#foo, you could write:

class B < A
  def foo(arg1, arg2)
    method(__method__).super_method.arity #=> 1
  end
end
like image 185
Stefan Avatar answered Oct 13 '22 10:10

Stefan