So the splat lets you pass an array as individual arguments. Or it can
work the other way around.
def foo(*args)
puts "arguments: #{args.size}"
end
foo(1,2,3) #=> 3
This example allows individual arguments to be passed in as an array.
This pattern is used a lot throughout rails.
In David's example:
my_array.delete_if {|a| [*a].include?("x") }
The "a" in that code will be each subarray of this main array. *a will
unpack that array into individual elements. And the wrapped around
*a will make a new array containing those elements.
The "a" in that code will be each subarray of this main array. *a will
unpack that array into individual elements. And the wrapped around
*a will make a new array containing those elements.
This all means that:
a = [1,2,3]
a == [*a] #=> true
So, the simpler way to do this is just:
my_array.delete_if {|a| a.include?("x") }
Except the array my_array doesn't consist only of subarrays; it also
contains some scalar elements. Doing it your way, you'd end up calling
(for example) 5.include?.
That's why I un-arrayed and re-arrayed each element.