Is there a way to access method arguments in Ruby?

后端 未结 11 1129
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 17:00

New to Ruby and ROR and loving it each day, so here is my question since I have not idea how to google it (and I have tried :) )

we have method

def f         


        
11条回答
  •  旧时难觅i
    2020-11-29 17:49

    You can define a constant such as:

    ARGS_TO_HASH = "method(__method__).parameters.map { |arg| arg[1].to_s }.map { |arg| { arg.to_sym => eval(arg) } }.reduce Hash.new, :merge"
    

    And use it in your code like:

    args = eval(ARGS_TO_HASH)
    another_method_that_takes_the_same_arguments(**args)
    

提交回复
热议问题