ruby 传递多个参数的ruby发送方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13795627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ruby send method passing multiple parameters
提问by rails4sandeep
Trying to create objects and call methods dynamically by
尝试通过动态创建对象和调用方法
Object.const_get(class_name).new.send(method_name,parameters_array)
which is working fine when
这是工作正常时
Object.const_get(RandomClass).new.send(i_take_arguments,[10.0])
but throwing wrong number of arguments 1 for 2 for
但是抛出错误数量的参数 1 for 2 for
Object.const_get(RandomClass).new.send(i_take_multiple_arguments,[25.0,26.0])
The Random Class defined is
定义的随机类是
class RandomClass
def i_am_method_one
puts "I am method 1"
end
def i_take_arguments(a)
puts "the argument passed is #{a}"
end
def i_take_multiple_arguments(b,c)
puts "the arguments passed are #{b} and #{c}"
end
end
Can someone help me on how to send mutiple parameters to a ruby method dynamically
有人可以帮助我如何动态地将多个参数发送到 ruby 方法
回答by
send("i_take_multiple_arguments", *[25.0,26.0]) #Where star is the "splat" operator
or
或者
send(:i_take_multiple_arguments, 25.0, 26.0)
回答by Tony Cronin
You can alternately call sendwith it's synonym __send__:
您可以交替send使用它的同义词调用__send__:
r = RandomClass.new
r.__send__(:i_take_multiple_arguments, 'a_param', 'b_param')
By the way* you can pass hashes as params comma separated like so:
顺便说一句*,您可以将散列作为逗号分隔的参数传递,如下所示:
imaginary_object.__send__(:find, :city => "city100")
or new hash syntax:
或新的哈希语法:
imaginary_object.__send__(:find, city: "city100", loc: [-76, 39])
According to Black, __send__is safer to namespace.
根据 Black 的说法,__send__命名空间更安全。
“Sending is a broad concept: email is sent, data gets sent to I/O sockets, and so forth. It's not uncommon for programs to define a method called send that conflicts with Ruby's built-in send method. Therefore, Ruby gives you an alternative way to call send:
__send__. By convention, no one ever writes a method with that name, so the built-in Ruby version is always available and never comes into conflict with newly written methods. It looks strange, but it's safer than the plain send version from the point of view of method-name clashes”
“发送是一个广泛的概念:发送电子邮件,将数据发送到 I/O 套接字,等等。程序定义一个名为 send 的方法与 Ruby 的内置 send 方法冲突的情况并不少见。因此,Ruby 为您提供了另一种调用 send: 的方法
__send__。按照惯例,从来没有人用这个名字写过一个方法,所以内置的 Ruby 版本总是可用的,永远不会与新编写的方法发生冲突。它看起来很奇怪,但从方法名称冲突的角度来看,它比普通的发送版本更安全”
Black also suggests wrapping calls to __send__in if respond_to?(method_name).
Black 还建议将调用封装到__send__in if respond_to?(method_name)。
if r.respond_to?(method_name)
puts r.__send__(method_name)
else
puts "#{r.to_s} doesn't respond to #{method_name}"
end
Ref: Black, David A. The well-grounded Rubyist. Manning, 2009. P.171.
Ref: Black, David A. 有充分根据的 Rubyist。曼宁,2009 年。第 171 页。
*I came here looking for hash syntax for __send__, so may be useful for other googlers. ;)
*我来这里是为了寻找 的哈希语法__send__,所以可能对其他谷歌员工有用。;)

