概要
Method#arity -> Fixnum
詳細
メソッドが受け付ける引数の数を返却。
ただし、メソッドが可変長引数を取る場合は負の数を返却します。
サンプルコード
require 'tbpgr_utils' class Hoge def hoge1;end def hoge2(a);end def hoge3(a, b);end def hoge4(*a);end def hoge5(a, *b);end def hoge6(a, &b);end def hoge7(a, *b, &c);end end hoge = Hoge.new bulk_puts_eval binding, <<-EOS hoge.method(:hoge1).arity hoge.method(:hoge2).arity hoge.method(:hoge3).arity hoge.method(:hoge4).arity hoge.method(:hoge5).arity hoge.method(:hoge6).arity hoge.method(:hoge7).arity EOS __END__ 下記はTbpgrUtils gemの機能 bulk_puts_eval https://rubygems.org/gems/tbpgr_utils https://github.com/tbpgr/tbpgr_utils
出力
hoge.method(:hoge1).arity # => 0 hoge.method(:hoge2).arity # => 1 hoge.method(:hoge3).arity # => 2 hoge.method(:hoge4).arity # => -1 hoge.method(:hoge5).arity # => -2 hoge.method(:hoge6).arity # => 1 hoge.method(:hoge7).arity # => -2