Ruby. method_missing + send对于带有命名参数的混合参数失败

iswrvxsc  于 11个月前  发布在  Ruby
关注(0)|答案(2)|浏览(170)

我有一段简单的Ruby代码,它是“代理模式”的实现,在最后一行失败了

class ExecuteProxy
    def initialize(controller_object)
      @controller_object = controller_object
    end

    def method_missing(method, *args)
      args.empty? ? @controller_object.send(method) : @controller_object.send(method, *args)
      return
    end
end

class MyClass
    def no_arghuments
        puts "no_arghuments"
    end
    def one_argument(arg1)
        puts "one_argument #{arg1}"
    end
    def one_argument_and_named(arg1, count:1)
        puts "one_argument #{arg1} and count #{count}"
    end
end

obj = MyClass.new
proxy = ExecuteProxy.new(obj)

proxy.no_arghuments
proxy.one_argument("test")
proxy.one_argument_and_named("test2")
proxy.one_argument_and_named("test2", count:2)

字符串
输出

no_arghuments
one_argument test
one_argument test2 and count 1
test1.rb:20:in `one_argument_and_named': wrong number of arguments (given 2, expected 1) (ArgumentError)
    from test1.rb:8:in `method_missing'
    from test1.rb:31:in `<main>'


如何解决这个问题?如果一个方法有普通参数+命名参数,我如何使用send将执行传递给不同的类?
更新.有工作代码,答案帮助,谢谢.

class ExecuteProxy
    def initialize(controller_object)
      @controller_object = controller_object
    end

    def method_missing(method, *args, **kw)
      @controller_object.send(method, *args, **kw)
      return
    end
end

class MyClass
    def no_arghuments
        puts "no_arghuments"
    end
    def one_argument(arg1)
        puts "one_argument #{arg1}"
    end
    def one_argument_and_named(arg1, count:1)
        puts "one_argument #{arg1} and count #{count}"
    end
    def only_named(count:, str:"")
        puts "only_named count #{count}, str: #{str}"
    end
    def manyarguments_and_named_args(arg1, arg2, arg3 = "def", count:1, some_other:"d")
        puts "manyarguments_and_named_args #{arg1}, #{arg2}, #{arg3} and count #{count}, some_other #{some_other}"
    end
end

obj = MyClass.new
proxy = ExecuteProxy.new(obj)

proxy.no_arghuments
proxy.one_argument("test")
proxy.one_argument_and_named("test2")
proxy.one_argument_and_named("test2", count:2)
proxy.only_named(count:3)
proxy.only_named(count:3,str:"string")
proxy.manyarguments_and_named_args("a1","a2")
proxy.manyarguments_and_named_args("a1","a2", "s3")
proxy.manyarguments_and_named_args("a1","a2", count:4)
proxy.manyarguments_and_named_args("a1","a2", count:4, some_other:nil)


输出

no_arghuments
one_argument test
one_argument test2 and count 1
one_argument test2 and count 2
only_named count 3, str: 
only_named count 3, str: string
manyarguments_and_named_args a1, a2, def and count 1, some_other d
manyarguments_and_named_args a1, a2, s3 and count 1, some_other d
manyarguments_and_named_args a1, a2, def and count 4, some_other d
manyarguments_and_named_args a1, a2, def and count 4, some_other

uoifb46i

uoifb46i1#

当你传递显式哈希作为最后一个参数时,它不算作关键字参数,你必须双重splat它们:

def one_argument_and_named(arg1, count: 1)
  puts "one_argument #{arg1} and count #{count}"
end

one_argument_and_named("test2", count: 2)      # good
one_argument_and_named(*["test2", count: 2])   # bad
one_argument_and_named("test2", {count: 2})    # bad
one_argument_and_named("test2", **{count: 2})  # good

字符串
你不需要检查是否有参数,如果参数为空,它们会发出噼啪声:

def method_missing(method, *args, **kw)
  @controller_object.send(method, *args, **kw)
end

# depending on your ruby version you could do this
# only if you don't need intermidiate access to args or kw
def method_missing(method, *, **)
  @controller_object.send(method, *, **)
end
# or
def method_missing(method, ...)
  @controller_object.send(method, ...)
end

xcitsw88

xcitsw882#

您需要调整在ExecuteProxy类的method_missing方法中处理参数的方式

def method_missing(method, *args)
  if args.last.is_a?(Hash)
    keyword_args = args.pop
    @controller_object.send(method, *args, **keyword_args)
  else
    @controller_object.send(method, *args)
  end
end

字符串

相关问题