Why is the splat used inside an array definition here?

試著忘記壹切 提交于 2019-12-19 03:35:30

问题


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?


回答1:


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]



回答2:


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/




回答3:


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



来源:https://stackoverflow.com/questions/1416777/why-is-the-splat-used-inside-an-array-definition-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!