为什么这里的数组定义中使用了 splat? [英] Why is the splat used inside an array definition here?
问题描述
def initialize(apps, catch=404)
@apps = []; @has_app = {}
apps.each { |app| add app }
@catch = {}
[*catch].each { |status| @catch[status] = true }
end
在此方法中来自 Rack::Cascade,splat(*)
在 [*catch]
代码中的作用是什么?
In this method from Rack::Cascade, what purpose does the splat(*)
serve in the [*catch]
code?
我认为在方法参数中使用了 splat 来指示您何时将拥有未指定数量的参数.
I thought a splat was used in method arguments to indicate when you are going to have an unspecified number of parameters.
splat在这里有不同的含义吗?
Does the splat have a different meaning here?
推荐答案
它为 catch 创建一个单一的平面数组
我不确定是否有人完全理解 splat 运算符.很多时候它会移除一层排列性",但不会移除最后一层.
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.
至少在这种情况下可以得到它.无论 catch
是单个数字还是数字数组,它都会为 catch 参数创建一个单一级别的数组.
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]
这篇关于为什么这里的数组定义中使用了 splat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!