ruby hash方法_Ruby中帶有示例的Hash.default(key = nil)方法

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方法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378994.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378994.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378994.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

回文數、括號匹配(棧操作)

回文數 “xyzyx”是一個回文字符串,所謂回文字符串就是指正讀反讀均相同的字符序列,如“席主席”、“記書記”、“aha”和“ahaha”均是回文,但“ahah”不是回文。輸入一行字符(僅包含小寫英文字母a~z)請判斷這行字符…

ijkplayer 消息循環處理過程分析

ijkplayer 消息循環處理過程分析簡介一、消息隊列初始化1、 initWithContentURLString函數2、 ijkmp_ios_create函數3、 ijkmp_create函數二、消息隊列的消息循環處理函數啟動1、prepareToPlay函數2、ijkmp_prepare_async函數3、ijkmp_prepare_async_l函數4、ijkmp_msg_loop函數…

json解析對應的value為null_徒手擼一個JSON解析器

Java大聯盟致力于最高效的Java學習關注作者 | 田小波cnblogs.com/nullllun/p/8358146.html1、背景JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。相對于另一種數據交換格式 XML,JSON 有著諸多優點。比如易讀性更好,占用空間更少等。在 …

[一]設計模式初探

模式,顧名思義,就是做一種事情的方法歸納,就經驗來說,做什么事情有個好的方法來應對都是可以事半功倍的,在軟件開發中何謂好的模式? 我認為好的模式簡單來說就是保證你應對需求變化的時候不用做更多的代碼修改&#x…

Gentoo - ssh-agent配置

現在使用類似github這樣的service&#xff0c;一般來說都會配置ssh key認證。所以使用ssh-agent來管理私鑰就變的必要。在Gentoo下是這么配置的&#xff1a;- sudo emerge -avt keychain- 編輯.bashrc&#xff0c;加入keychain <private key 1 path> ... <private key…

java 方法 示例_Java ArrayDeque offerFirst()方法與示例

java 方法 示例ArrayDeque類offerFirst()方法 (ArrayDeque Class offerFirst() method) offerFirst() Method is available in java.lang package. offerFirst()方法在java.lang包中可用。 offerFirst() Method is used to add the given element at the front of this deque. …

平院Python習題

在讀寫文件之前&#xff0c;用于創建文件對象的函數是&#xff08; A &#xff09;。 A&#xff0e; open B&#xff0e; create C&#xff0e; file D&#xff0e;folder 解析&#xff1a; open(file, mode‘r’, buffering-1, encodingNone, errorsNone, newlineNone, close…

搭建srs服務器(rtmp)

搭建srs服務器&#xff08;rtmp&#xff09; 目錄:1、下載srs源碼&#xff08;從碼云上&#xff09;&#xff1a;2、 切換到srs.oschina&#xff1a;3、 這里使用3.0版本&#xff1a;4、 切換到trunk&#xff1a;5、 編譯&#xff1a;6、 啟動&#xff1a;7、查看日志輸出&…

MOSS信息管理策略定制(MOSS custom policies)

MOSS引入了信息管理策略&#xff0c;通過給文檔庫或列表附加一些策略可以實現一些自動化的功能&#xff0c;如自動給文檔打標簽&#xff0c;強制文檔的審核&#xff0c;啟用文檔的過期&#xff0c;甚至可以在用戶打印文檔時插入條形碼。難么&#xff0c;這么好用的功能是怎么實…

2560介紹_炒股高手收益翻10倍,只因妙用這一招2560戰法,看了都不虧了

(本文由公眾號越聲研究(yslc927yj)整理&#xff0c;僅供參考&#xff0c;不構成操作建議。如自行操作&#xff0c;注意倉位控制和風險自負。)選股是每個剛入市的新股民需掌握的技巧&#xff0c;通過資金選股了解資金流向&#xff0c;善用工具輕松選股&#xff0c;同時多留意行業…

二進制搜索樹_將排序的數組轉換為二進制搜索樹

二進制搜索樹Problem statement: 問題陳述&#xff1a; Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 給定一個數組&#xff0c;其中元素按升序排序&#xff0c;請將其轉換為高度平衡的BST。 For this problem, a he…

rtmp協議分析(三次握手)

RTMP詳細分析(Message 消息&#xff0c;Chunk分塊) librtmp分析&#xff08;發送數據包處理&#xff09; librtmp分析&#xff08;接收數據包處理&#xff09; RTMP協議是Real Time Message Protocol(實時信息傳輸協議)的縮寫&#xff0c;它是由Adobe公司提出的一種應 用層的協…

OpenAPI系列: 六、OpenAPI策略分析

一、如何注冊 為什么要注冊&#xff1f;訪問 OpenAPI必須擁有Consumer Key和Consumer Secret。 如何注冊&#xff1f;要獲取Consumer Key及Consumer Secret&#xff0c;需要消費方&#xff08;Consumer&#xff09;向服務提供方申請注冊&#xff0c;服務提供方審核通過后會向消…

壓縮、解壓 解決 客戶端查詢大批量數據時等待時間過長的問題

在項目中查詢時&#xff0c;因數據量大&#xff0c;導致網絡傳輸很慢&#xff0c;這就需要在服務器端查詢出的數據進行壓縮處理&#xff0c;后傳輸完了在客戶端進行解壓處理&#xff08;此為在Silverlight中壓縮與解壓&#xff09;&#xff1b; 具體方法如下&#xff1a; using…

C---已知正整數n是兩個不同的質數的乘積,試求出較大的那個質數。

已知正整數n是兩個不同的質數的乘積&#xff0c;試求出較大的那個質數。 思路&#xff1a;由題意可知&#xff0c;n為兩個質數之積&#xff0c;也就是說只要找到一個數能夠被n整除&#xff0c;這個數一定是質數&#xff01;&#xff01;&#xff01;2為最小的質數&#xff0c;…

isnumeric_Python字符串| isnumeric()方法與示例

isnumericisnumeric() is an in-built method in Python, which is used to check whether a string contains only numeric values or not. isnumeric()是Python中的內置方法&#xff0c;用于檢查字符串是否僅包含數字值。 Numeric contain all decimal characters and the f…

合并文件夾中子目錄_01 Linux之統計文件夾中文件個數以及目錄個數

案例分析&#xff1a;今天遇到了一個需要統計路徑下目錄個數的問題如果一個一個的去數會很麻煩&#xff0c;找到了一篇文章剛好提到這個&#xff0c;于是我將方法整理了一下。該方法的鏈接&#xff1a;Linux統計文件夾中文件個數以及目錄個數_SG匚hang的博客-CSDN博客_linux統計…

關于Makefile,Makefile.in,Makefile.am,Configure功能及相互關系的問題

目錄makefile寫法1. 簡介2. 上路之前3. 一個簡單的例子4.說明&#xff1a;4.1、autoscan4.2、 configure.scan4.3、aclocal4.4、autoconf4.5、Makefile.am4.6、 automake4.7、Makefilemakefile寫法 在 Unix 上寫程式的人大概都碰過 Makefile&#xff0c;尤其是用 C 來開發程式…

修改主鍵的SQL

declare defname varchar(100)declare cmd varchar(500)declare tablename varchar(100)declare keyname varchar(100) Set tablenameTemp1Set keynameid --需要設置的key,分隔 select defname name FROM sysobjects so JOIN sysconstraints sc ON so.id sc.constid …

西安理工大學863(轉載)

原創&#xff1a;https://blog.csdn.net/mzj15101229871/article/details/107613162 &#xff08;博主總結的很完整&#xff0c;很厲害&#xff0c;本人為了查看方便&#xff0c;才轉載的。本人只是個小白~&#xff09; 第一章 緒論 考試大綱 1&#xff09;了解數據元素、數…