def initialize(apps, catch=404)
@apps = []; @has_app = {}
apps.each { |app| add app }
@catch = {}
[*catch].each { |status| @catch[status] = true }
end
In this method from Rack::Cascade, what purpose does the splat(*)
serve in the [*catch]
code?
I thought a splat was used in method arguments to indicate when you are going to have an unspecified number of parameters.
Does the splat have a different meaning here?
Another way to look at it: an l-value splat is greedy and contains as many corresponding r-values as possible.
I think the best way to understand this is to look what is happening in
irb
.So let's initialize an empty hash,
@catch
:Now let's see what happens when the parameter
catch
goes to it's default value of404
:This gives us a better idea of what is going on. We can see that the
splat
operator is being used to build aHash
of responses. The response numbers are being used as akey
and true is set as thevalue
. So no matter how many items we have in catch, we can still build aHash
.I hope this helps. Here is a link that helped me out a little bit:
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/
It creates a single flat array for catch
I'm not sure anyone completely understands the splat operator. Many times it removes one level of "arrayness", but it won't remove the last level.
It is possible to get it in this one case, at least. It creates a single level of array for the catch parameter regardless of whether
catch
is a single number or an array of numbers.