【发布时间】:2021-05-04 15:41:13
【问题描述】:
我有一个proc,设置如下:
@method_to_call = Proc.new { || {:method=>'some_method',:user_id=>1 } }
现在我想用参数user_id调用方法some_method
def some_method(user_id)
# does something
end
关键是proc也可以有不同的参数,例如:
@method_to_call = Proc.new { || {:method=>'some_method_two',:user_id=>1, :app_id=>2 } }
会调用:
def some_method_two(user_id,app_id)
# do something
end
我目前有一个方法如图:
def handle_action
parts = @method_to_call.call
curr_method = parts[:method]
if curr_method == "some_method"
some_method parts[:user_id]
elsif curr_method == "some_method_two"
some_method_two parts[:user_id], parts[:app_id]
end
end
但我想要...
def handle_action
# call method in proc and pass parameters stored in proc dynamically
end
【问题讨论】: