LoginSignup
6
3

More than 5 years have passed since last update.

Ruby のメソッドが取れる引数のパターンを列挙する

Posted at

メソッドが取れる引数のパターンをざっくり列挙するスクリプトを書いてみました。

arg_types = %w(req opt=2 keyreq: key:4 *rest **keyrest &block)

1.upto(arg_types.size) do |c|
  puts
  puts "--------------- arg types num #{c} ---------------"
  arg_types.permutation(c).each do |l|
    code = "def func(#{l.join(', ')}) end"
    begin
      eval(code)
      puts code
    rescue SyntaxError
    end
  end
end

実行結果

--------------- arg types num 1 ---------------
def func(req) end
def func(opt=2) end
def func(keyreq:) end
def func(key:4) end
def func(*rest) end
def func(**keyrest) end
def func(&block) end

--------------- arg types num 2 ---------------
def func(req, opt=2) end
def func(req, keyreq:) end
def func(req, key:4) end
def func(req, *rest) end
def func(req, **keyrest) end
.
.
.
--------------- arg types num 7 ---------------
def func(req, opt=2, *rest, keyreq:, key:4, **keyrest, &block) end
def func(req, opt=2, *rest, key:4, keyreq:, **keyrest, &block) end
def func(opt=2, *rest, req, keyreq:, key:4, **keyrest, &block) end
def func(opt=2, *rest, req, key:4, keyreq:, **keyrest, &block) end

抜けてるパターンもあるのですが、大体のパターンがわかります。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3