define_method with predefined keyword arguments

2020-07-13 10:12发布

问题:

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