为什么在数组之前需要星号? [英] Why do I need asterisk before an array?
问题描述
我不知道这是一个哈希问题还是一个数组问题,但我不明白为什么在第三个示例中需要使用星号(*)符号来获取用数据填充的散列。没有它,它会输出一个空的散列。
# - * - coding:utf-8 - * -
require 'pp'
pp [[:first_name,'Shane'],[:last_name,'Harvie']]
#=> [[:first_name,Shane],[:last_name,Harvie]]
[[:first_name,'Shane'],[:last_name,'Harvie']]。flatten
#=> [:first_name,Shane,:last_name,Harvie]
Hash [* [[:first_name,'Shane'],[:last_name,'Harvie']]。flatten]
#=> {:first_name =>Shane,:last_name =>Harvie}
Hash [[[:first_name,'Shane'],[:last_name,'Harvie']]。 flatten]
#=> {}
以下是该方法的解释。
------------------------------------- -------------------------- Hash :: []
Hash [[key => |,value] *] => ;哈希
从Ruby 1.8
---------------------------------- --------------------------------------
创建一个填充给定的新哈希对象。相当于
的文字+ {_key_,_value_,...} +。键和值出现在
对中,所以必须有偶数个参数。
Hash [a,100,b,200]#=> {a=> 100,b=> 200}
哈希[a=> 100,b=> 200]#=> {a=> 100,b=> 200}
{a=> 100,b=> 200}#=> {a=> 100,b=> 200}
[]
运算符一个实际列表,以将其转换为散列的键/值对。 (请参阅下面的链接,以获取splat操作符的简短描述(实际上可以这样做)(将数组展开为列表)或反向(将列表收集到数组中)。) 按照上面的方式,您给 Hash []
一个奇数项,即单个数组。 (想想 [[:first_name,'Shane'],[:last_name,'Harvie']]。flatten
产生了什么,它会产生 [: firstname,'Shane',:last_name,'Havie']
。)正如你引用的文档所说, []
操作符必须有一个偶数元素的数量。请注意,以下(尽管无用)确实有效:
>> Hash [[[:first_name,'Shane'],[:last_name,'Harvie']]。flatten,1]
=> {[:first_name,Shane,:last_name,Harvie] => 1}
(我不清楚为什么当你使用上面的代码时,你没有得到奇数个Hash参数错误 - 就像你尝试 Hash [1] $ c $ )
一个简单的例子可能会使它更清晰。首先,传入一个项目,一个数组。使用 *
打开 Hash []
项目列表:
>> Hash [[''foo','bar','bizz','buzz']]
=> {}
>>哈希[* ['foo','bar','bizz','buzz']]
=> {foo=>bar,bizz=>buzz}
请参阅此博客帖子以获得更完整的解释。您可能还会发现这篇关于图片的简短说明运营商有用。
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 it, it outputs an empty hash.
# -*- coding: utf-8 -*-
require 'pp'
pp [[:first_name, 'Shane'], [:last_name, 'Harvie']]
# => [[:first_name, "Shane"], [:last_name, "Harvie"]]
pp [[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten
# => [:first_name, "Shane", :last_name, "Harvie"]
pp Hash[*[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten]
# => {:first_name=>"Shane", :last_name=>"Harvie"}
pp Hash[[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten]
# => {}
Below is an explanation of the method.
--------------------------------------------------------------- Hash::[]
Hash[ [key =>|, value]* ] => hash
From Ruby 1.8
------------------------------------------------------------------------
Creates a new hash populated with the given objects. Equivalent to
the literal +{ _key_, _value_, ... }+. Keys and values occur in
pairs, so there must be an even number of arguments.
Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
{ "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
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.
这篇关于为什么在数组之前需要星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!