Page MenuHomePhabricator
Paste P15953

splat
ActivePublic

Authored by jbond on May 12 2021, 5:02 PM.
Tags
None
Referenced Files
F34451972: splat
May 12 2021, 5:02 PM
Subscribers
None
def no_splat(splat, &block)
block.call(splat)
end
def splat_call(splat, &block)
block.call(*splat)
end
def splat_param(*splat, &block)
block.call(splat)
end
%w[no_splat splat_call splat_param].each do |func|
puts "testing: #{func}\n"
result = ''
begin
send(func, [1,2]) do |recv|
result = recv.inspect
end
rescue
result = 'Failed'
end
puts "Array:\t\t#{result.inspect}"
begin
send(func, [1,2]) do |*recv|
result = recv.inspect
end
rescue
result = 'Failed'
end
puts "Array splat recv:\t\t#{result.inspect}"
begin
send(func, *[1,2]) do |recv|
result = recv.inspect
end
rescue
result = 'Failed'
end
puts "Positional:\t\t#{result.inspect}"
begin
send(func, *[1,2]) do |*recv|
result = recv.inspect
end
rescue
result = 'Failed'
end
puts "Positional splat recv:\t\t#{result.inspect}"
puts "\n\n"
end

Event Timeline

$ ruby test.rb                                                                                                                                   [18:17:09]
testing: no_splat
Array:                                  "[1, 2]"
Array splat recv:                "[[1, 2]]"
Positional:                          "Failed"
Positional splat recv:          "Failed"


testing: splat_call
Array:                                    "1"
Array splat recv:                 "[1, 2]"
Positional:                           "Failed"
Positional splat recv:          "Failed"


testing: splat_param
Array:                                   "[[1, 2]]"
Array splat recv:                 "[[[1, 2]]]"
Positional:                            "[1, 2]"
Positional splat recv:          "[[1, 2]]"