Why do I need asterisk before an array?

前端 未结 1 1150
时光取名叫无心
时光取名叫无心 2021-02-07 15:59

I have no idea if this is a Hash issue or an Array issue, but I don\'t figure out why asterisk (*) sign is required in the third example to get a hash filled with data. Without

1条回答
  •  旧巷少年郎
    2021-02-07 16:31

    The splat operator (that is, the *) turns what would otherwise be an array into a list for assignment within the hash. You have to give the [] operator an actual list to turn into the key/value pairs of a hash. (See below for a link to a short description of the splat operator which actually can do this (unwind an array into a list) or the reverse (gather a list into an array).)

    The way you did it above, you give Hash[] an odd number of items, namely a single array. (Think of what [[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten produces. It yields [:first_name, 'Shane', :last_name, 'Havie'].) As the docs you quoted say, the [] operator must have an even number of elements. Note that the following (though useless) does work:

    >> Hash[[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten, 1]
    => {[:first_name, "Shane", :last_name, "Harvie"]=>1}
    

    (It's unclear to me why you don't get the "odd number of arguments for Hash" error when using the code you have above - as you do if you try Hash[1].)

    A simpler example may make it clearer. First, passing in one item, an array. The opening up the array with * to hand Hash[] a list of items:

    >> Hash[['foo', 'bar', 'bizz', 'buzz']]
    => {}
    >> Hash[*['foo', 'bar', 'bizz', 'buzz']]
    => {"foo"=>"bar", "bizz"=>"buzz"}
    

    See this blog post for a fuller explanation. You might also find this short write-up about the splat operator useful.

    0 讨论(0)
提交回复
热议问题