define_method with predefined keyword arguments

随声附和 提交于 2019-12-06 02:47:55

问题


I want to define a method that takes keyword arguments. I would like it to raise when keyword arguments are not provided, and I can code this myself - but ideally I would like to let Ruby do that for me. Also I would like to be able to inspect the freshly defined method using Method#parameters. If I use a shorthand double-splat (like **kwargs) the actual structure I expect is not visible to parameters.

I can of course do this:

define_method(:foo) do | foo:, bar: |
  # ...
end

which achieves the desired result:

method(:foo).parameters
# => [[:keyreq, :foo], [:keyreq, :bar]]

but I cannot pass those arguments programmatically, they have to be literally placed in the code. Is there a way I could bypass this?


回答1:


You have to use eval to define arguments dynamically (not just keyword arguments), e.g. using class_eval:

class MyClass
  name = :foo
  args = [:bar, :baz]
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
    def #{name}(#{args.map { |a| "#{a}:" }.join(', ')}) # def foo(bar:, baz:)
      [#{args.join(', ')}]                              #   [bar, baz]
    end                                                 # end
  METHOD
end

MyClass.new.foo(bar: 1, baz: 2)
#=> [1, 2]

MyClass.instance_method(:foo).parameters
#=> [[:keyreq, :bar], [:keyreq, :baz]]


来源:https://stackoverflow.com/questions/31810695/define-method-with-predefined-keyword-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!