rspec - Is there a way without a (*)splat argument to pass multiple arguments in Ruby? -
i need write method takes unknown number of arguments (hence *splat) passes yields_with_args
spec.
the code:
def eval_block(*args, &block) raise "no block given!" if block.nil? block.call(args) end
the rspec:
it "passes arguments block" expect |block| eval_block(1, 2, 3, &block) end.to yield_with_args(1, 2, 3) end end
it works, yields array *splat creates: [1,2,3]
vs 1,2,3
, , therefore doesn't pass rspec. there way pass on multiple arguments through method in ruby?
replace
block.call(args)
with
block.call(*args)
splat has 2 functions: collecting arguments array when in definition, , distributing array arguments in calls. 2 inverse operations: if expect transparent operation (three arguments go in, 3 arguments go out), should distribute collected.
Comments
Post a Comment