Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the splat used inside an array definition here?

Tags:

ruby

splat

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?

like image 732
uzo Avatar asked Sep 13 '09 03:09

uzo


2 Answers

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.

>> t = [*404]
=> [404]
>> t = [*[404,405,406]]
=> [404, 405, 406]
like image 144
DigitalRoss Avatar answered Oct 22 '22 11:10

DigitalRoss


I think the best way to understand this is to look what is happening in irb.

So let's initialize an empty hash, @catch:

>> @catch = {}
=> {}
>> @catch.class
=> Hash

Now let's see what happens when the parameter catch goes to it's default value of 404:

>> catch=404
=> 404
>> [*catch].each { |status| @catch[status] = true }
=> [404]
>> @catch[404]
=> true

This gives us a better idea of what is going on. We can see that the splat operator is being used to build a Hash of responses. The response numbers are being used as a key and true is set as the value. So no matter how many items we have in catch, we can still build a Hash.

>> catch=[404, 301, 302, 303, 403]
=> [404, 301, 302, 303, 403]
>> [*catch].each { |status| @catch[status] = true }
=> [404, 301, 302, 303, 403]
>> @catch
=> {302=>true, 303=>true, 403=>true, 404=>true, 301=>true}

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/

like image 23
jluebbert Avatar answered Oct 22 '22 13:10

jluebbert