ruby hash params = [] params << {:param =>:testString,type :: string}(ruby hash params = [] params << {:param => :testString, type : :string})
我遇到了这段代码,无法理解它实际上是做什么的。 你能解释一下这是做什么的:
params = [] params << {:param => :testString, type : :string} params << {:param => :testJson, type : :string}I came across this code, cannot comprehend what it actually does. Could you please explain me what this does:
params = [] params << {:param => :testString, type : :string} params << {:param => :testJson, type : :string}最满意答案
正确的代码是(您的代码中存在语法错误):
params << {:param => :testString, type: :string} # => [{:param=>:testString, :type=>:string}] params << {:param => :testJson, type: :string} # => [{:param=>:testString, :type=>:string}, {:param=>:testJson, :type=>:string}]代码只是将您定义的Hash添加到Array 。 方法<<将指定的值附加到Array的末尾,并且它不验证该值是否已存在于Array 。
请不要混合旧版本的哈希对(使用=> )和新版本(使用=> ,因为它的视图非常奇怪。 所以要么:
params << {:param => :testString, type => :string}要么
params << {param: :testString, type: :string}The correct code is (you had the syntax mistake in your code):
params << {:param => :testString, type: :string} # => [{:param=>:testString, :type=>:string}] params << {:param => :testJson, type: :string} # => [{:param=>:testString, :type=>:string}, {:param=>:testJson, :type=>:string}]The code just adds the Hash you defined into the Array. The method << will append the specified value to the end of the Array, and it doesn't verify weither the value is already exists in the Array.
Please, don't mix old version of hash pair (using =>), and a new one (using :) because its view is very strange. So either:
params << {:param => :testString, type => :string}or
params << {param: :testString, type: :string}更多推荐
发布评论