ruby hash方法_Ruby中带有示例的Hash.values方法
ruby hash方法
哈希值方法 (Hash.values Method)
In this article, we will study about Hash.values Method. The working of the method can be assumed because of its very common name but there exist some hidden complexities too. Let us read its definition and understand its implementation with the help of syntax and program codes.
在本文中,我們將研究Hash.values方法 。 可以假定該方法的工作是由于其非常通用的名稱,但也存在一些隱藏的復雜性。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。
Method description:
方法說明:
This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns an array that contains all the values present in the hash object. This method traverses the whole hash object and stores the values inside the array to be returned with a separate index. The Hash.values() method will return an empty array instance if there is no value present inside the hash object.
此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 此方法的工作方式是返回一個包含哈希對象中存在的所有值的數組。 此方法遍歷整個哈希對象,并將值存儲在要返回的數組中并帶有單獨的索引。 如果哈希對象內部不存在任何值,則Hash.values()方法將返回一個空數組實例。
Syntax:
句法:
Hash_object.valuesArgument(s) required:
所需參數:
This method does not take any parameter. It returns a hash which contains all the values of hash object.
此方法不帶任何參數。 它返回一個包含哈希對象所有值的哈希。
Example 1:
范例1:
=beginRuby program to demonstrate Hash.values method =end hsh = {"colors" => "red","city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}puts "Hash.values implementation"ary = hsh.valuesputs "Values present in the hash are:"ary.each do |val|puts val endOutput
輸出量
Hash.values implementation Values present in the hash are: red Nainital Grapes red ladooExplanation:
說明:
In the above code, you can observe that we can get all the values present inside a particular hash object with the help of the Hash.values() method. You can see that all the values are stored inside an array object which can be displayed.
在上面的代碼中,您可以觀察到,借助于Hash.values()方法 ,我們可以獲取特定哈希對象內存在的所有值。 您會看到所有值都存儲在可以顯示的數組對象中。
Example 2:
范例2:
=beginRuby program to demonstrate Hash.values method =end hsh = {"colors" => ["red","blue","green"],"city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}puts "Hash.values implementation"ary = hsh.valuesputs "Values present in the hash are:"puts "#{ary}"Output
輸出量
Hash.values implementation Values present in the hash are: [["red", "blue", "green"], "Nainital", "Grapes", "red", "ladoo"]Explanation:
說明:
In the above code, you can observe that you can fetch all the values from a hash object with the help of the Hash.values() method. In the above code, you can observe that a multidimensional array object has been returned because the hash object was containing an array of values stored inside a particular key.
在上面的代碼中,您可以觀察到可以借助Hash.values()方法從哈希對象中獲取所有值。 在上面的代碼中,您可以觀察到已返回多維數組對象,因為哈希對象包含存儲在特定鍵中的值的數組。
翻譯自: https://www.includehelp.com/ruby/hash-values-method-with-example.aspx
ruby hash方法
總結
以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.values方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stl vector 函数_vector
- 下一篇: Java LinkedList addF