Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种没有(*)splat参数的方法可以在Ruby中传递多个参数?_Ruby_Rspec_Splat - Fatal编程技术网

有没有一种没有(*)splat参数的方法可以在Ruby中传递多个参数?

有没有一种没有(*)splat参数的方法可以在Ruby中传递多个参数?,ruby,rspec,splat,Ruby,Rspec,Splat,我需要编写一个方法,该方法接受未知数量的参数(因此是*splat),但它传递一个generations\u和\u argsspec 守则: def eval_block(*args, &block) raise "NO BLOCK GIVEN!" if block.nil? block.call(args) end rspec: it "passes the arguments into the block" do expect do |block|

我需要编写一个方法,该方法接受未知数量的参数(因此是*splat),但它传递一个
generations\u和\u args
spec

守则:

def eval_block(*args, &block)
    raise "NO BLOCK GIVEN!" if block.nil?
       block.call(args)
end
rspec:

it "passes the arguments into the block" do
      expect do |block|
        eval_block(1, 2, 3, &block)
      end.to yield_with_args(1, 2, 3)
    end
end
它可以工作,但它会生成*splat创建的数组:
[1,2,3]
vs
1,2,3
,因此不会通过rspec。在Ruby中,有没有其他方法可以通过一个方法传递多个参数

更换

block.call(args)

Splat有两个功能:在定义中收集数组的参数,在调用中将数组分发给参数。这两种操作是反向操作:如果您希望使用透明操作(三个参数进入,三个参数退出),则应该分发收集的内容

block.call(*args)