Why is the splat used inside an array definition h

2020-03-03 09:26发布

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?

标签: ruby splat
3条回答
Bombasti
2楼-- · 2020-03-03 09:35

Another way to look at it: an l-value splat is greedy and contains as many corresponding r-values as possible.

查看更多
狗以群分
3楼-- · 2020-03-03 09:38

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/

查看更多
混吃等死
4楼-- · 2020-03-03 09:40

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]
查看更多
登录 后发表回答