Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby哈希参数=[]参数<&书信电报;{:param=>;:testString,类型::string}_Ruby_Arrays_Hash_Types - Fatal编程技术网

ruby哈希参数=[]参数<&书信电报;{:param=>;:testString,类型::string}

ruby哈希参数=[]参数<&书信电报;{:param=>;:testString,类型::string},ruby,arrays,hash,types,Ruby,Arrays,Hash,Types,我遇到了这个代码,无法理解它的实际功能。你能给我解释一下这是干什么的吗 params = [] params << {:param => :testString, type : :string} params << {:param => :testJson, type : :string} params=[] 参数:testString,类型::string} params:testJson,类型::string} 正确的代码是(您的代码中有语法错误):

我遇到了这个代码,无法理解它的实际功能。你能给我解释一下这是干什么的吗

params = [] 
params << {:param => :testString, type : :string}
params << {:param => :testJson, type : :string}
params=[]
参数:testString,类型::string}
params:testJson,类型::string}
正确的代码是(您的代码中有语法错误):

params该方法将项附加到数组的末尾

a = []
a << 1
a << 2
a # => [1, 2]

下面是Thank Majioa的详细信息,这与之前粘贴的代码相同,params=[]params:testString,:type=>:string}params:testJson,:type=>:string}@Swamy在
之前对代码进行了一些更改,空格已被删除
params << {:param => :testString, type => :string}
params << {param: :testString, type: :string}
a = []
a << 1
a << 2
a # => [1, 2]
params << {:param => :testString, type : :string}
#                                     ^
params = [] #instantiate new array named params
params << {:param => :testString, type: :string} #uses the << method to push a hash into the params array
params << {:param => :testJson, type: :string} #uses the << method to push a hash into the params array
[{:param => :testString, type: :string},{:param => :testJson, type: :string}]