ruby hash params = [] params :testString,type :: string}(ruby hash params = [] params :testString,

编程入门 行业动态 更新时间:2024-10-23 05:46:01
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}

更多推荐

本文发布于:2023-08-04 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:params   hash   ruby   string   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!