Why is the splat used inside an array definition here?

前端 未结 3 1110
天涯浪人
天涯浪人 2021-01-05 20:26
def initialize(apps, catch=404)
  @apps = []; @has_app = {}
  apps.each { |app| add app }

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


        
3条回答
  •  心在旅途
    2021-01-05 21:08

    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]
    

提交回复
热议问题