6
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Organization

メソッド内でメソッドの引数をHashで取得する

phpだとget_func_argsとかで引数の一覧取れたのにと思ったんですが、rubyでは無いのかなと。
一応以下の様な感じで取得できました。

def hoge(a,b:)
  args = self.method(__callee__).parameters.map do |arg_type,arg|
    [ arg, eval(arg.to_s) ]
  end.to_h
  p args
  #=> {:a=>1, :b=>2}
end

hoge(1, b: 2)

rubyだしもっと良い書き方ありそうですよね。
何かあればコメントください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
6
Help us understand the problem. What are the problem?