6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-05-21

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だしもっと良い書き方ありそうですよね。
何かあればコメントください。

6
6
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
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?