ruby hash方法
Hash.default(key = nil)方法 (Hash.default(key=nil) Method)
In this article, we will study about Hash.default(key=nil) Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.
在本文中,我們將研究Hash.default(key = nil)方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。
Method description:
方法說明:
This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it returns the default value but now you will be thinking when this method will return the default value. This method will return the default value when the user is trying to access the value of key or simply key which is not available in the hash or you can say that which is not a part of the hash object. The method will print the default value instead of returning 'nil'.
此方法是在ruby庫中定義的公共實例方法,特別是針對Hash類。 此方法以返回默認值的方式工作,但是現在您將考慮此方法何時將返回默認值。 當用戶嘗試訪問哈希值中不可用的key值或key值時,此方法將返回默認值,或者您可以說那不是哈希對象的一部分。 該方法將打印默認值,而不是返回'nil' 。
Syntax:
句法:
Hash_object.default
or
Hash_object.default(object)
Argument(s) required:
所需參數:
Passing value in this method is optional. If you are passing a value then that value can be an object of any class.
在此方法中傳遞值是可選的。 如果要傳遞值,則該值可以是任何類的對象。
Example 1:
范例1:
=begin
Ruby program to demonstrate default method
=end
hsh = Hash.new("Not available")
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash default implementation"
puts "Hash contents are : #{hsh}"
puts "default value : #{hsh.default}"
Output
輸出量
Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : Not available
Explanation:
說明:
In the above code, you can observe that we have set a default value with the help of the new method. We are accessing that default value with the help of the default method. The default value is "Not available" and this value will be returned whenever the key is not found in the hash object.
在上面的代碼中,您可以看到我們已經在new方法的幫助下設置了默認值。 我們正在借助默認方法訪問該默認值。 默認值為“不可用” ,只要在哈希對象中找不到密鑰,就會返回此值。
Example 2:
范例2:
=begin
Ruby program to demonstrate default method
=end
hsh = Hash.new { |hash, key| hsh[key] = "Hello : #{key}" }
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash default implementation"
puts "Hash contents are : #{hsh}"
puts "default value : #{hsh.default(2)}"
Output
輸出量
Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : Hello : 2
Explanation:
說明:
In the above code, you can observe that we have set a default value with the help of the new method by passing a block into it. We are accessing that default value with the help of the default method. The default value is "Hello: key" and this value will be returned whenever the key is not found in the hash object. We are passing the key along with the method and that key will be returned along with the value when the key is missing.
在上面的代碼中,您可以觀察到,借助于將新的塊傳遞給新方法,我們已經設置了默認值。 我們正在借助默認方法訪問該默認值。 默認值為“ Hello:key” ,只要在哈希對象中未找到該鍵,就會返回該值。 我們將鍵與方法一起傳遞,當鍵丟失時,鍵將與值一起返回。
翻譯自: https://www.includehelp.com/ruby/hash-default-key-nil-method-with-example.aspx
ruby hash方法