日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby hash方法_Hash.fetch()方法以及Ruby中的示例

發布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby hash方法_Hash.fetch()方法以及Ruby中的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby hash方法

Hash.fetch()方法 (Hash.fetch() Method)

In this article, we will study about Hash.fetch() 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.fetch()方法 。 可以借助其名稱來預測此方法的工作,但是它并不像看起來那樣簡單。 好了,我們將在其余內容中借助其語法和程序代碼來理解此方法。

Method description:

方法說明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method requires keys whose values are fetched by this method. This method works in the way that it returns the value or values from the hash object for a given key or keys. In case the key is not found in the hash instance then the result is dependent upon the type of method call done. The possible method calls are listed below:

此方法是在ruby庫中定義的公共實例方法,特別是針對Hash類。 此方法需要通過此方法獲取其值的鍵。 此方法的工作方式是,它從給定鍵或多個鍵的哈希對象中返回一個或多個值。 如果在哈希實例中找不到鍵,則結果取決于完成的方法調用的類型。 下面列出了可能的方法調用:

  • Without any other argument: If no other argument is provided and the key is not found then the method will throw a "KeyError" exception.

    沒有任何其他自變量 :如果未提供其他自變量且未找到鍵,則該方法將引發“ KeyError”異常。

  • With other arguments (s): The other argument is considered as the default argument. If the key is not found then that default argument will be returned from the method.

    與其他參數一起使用 :另一個參數被視為默認參數。 如果找不到該鍵,則將從該方法返回該默認參數。

  • With block: If the block is provided and the key is not found then that block will be run and the result will be returned in case the key is not found.

    With塊 :如果提供了該塊,但未找到鍵,則該塊將運行,如果未找到鍵,則將返回結果。

Syntax:

句法:

Hash_object.fetch(…,[default]);orHash_object_fetch(key(s))orHash_object_fetch{|key| block}

Argument(s) required:

所需參數:

There is no restriction upon passing the arguments. You can pass arguments as per your requirement. The last argument will always be considered as the default argument.

傳遞參數沒有限制。 您可以根據需要傳遞參數。 最后一個參數將始終被視為默認參數。

Example 1:

范例1:

=beginRuby program to demonstrate fetch method =end hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.fetch implementation"puts "Enter the key you want to search:" ky = gets.chompif(hash1.fetch(ky))puts "Key found successfully. The value is #{hash1.fetch(ky)}" elseputs "No key found" end

Output

輸出量

RUN 1: Hash.fetch implementation Enter the key you want to search:color Key found successfully. The value is BlackRUN 2: Hash.fetch implementation Enter the key you want to search:velvet key not found: "velvet" (repl):12:in `fetch' (repl):12:in `<main>'

Explanation:

說明:

In the above code, you can simply observe that we have invoked the method with a key which is being asked by the user. In Run 2, you may see that the exception is thrown when the key is not found in the hash.

在上面的代碼中,您可以簡單地觀察到我們已經使用了用戶要求的密鑰來調用該方法。 在運行2中,您可能會看到在哈希中找不到鍵時引發了異常。

Example 2:

范例2:

=beginRuby program to demonstrate fetch method =end hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.fetch implementation"puts "Enter the key you want to search:" ky = gets.chompputs "The value of #{ky} is #{hash1.fetch(ky,"Not found")}"

Output

輸出量

Hash.fetch implementation Enter the key you want to search:cloth The value of cloth is Not found

Explanation:

說明:

In the above code, you can observe that we have passed a default argument along with the key inside the method. You can see that the default object is returned when the key is not found in the hash instance.

在上面的代碼中,您可以觀察到我們已經在方法內部傳遞了默認參數以及鍵。 您可以看到,在哈希實例中未找到鍵時,將返回默認對象。

Example 3:

范例3:

=beginRuby program to demonstrate fetch method =end hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}puts "Hash.fetch implementation"puts "Enter the key you want to search:" ky = gets.chompputs "The value of #{ky} is #{hash1.fetch(ky){|ky| "Sorry! #{ky} is missing"}}"

Output

輸出量

Hash.fetch implementation Enter the key you want to search:cloth The value of cloth is Sorry! cloth is missing

Explanation:

說明:

In the above code, you can observe that we are passing a block along with the method. That block has been run and its value has been returned when the key is not found in the hash object.

在上面的代碼中,您可以觀察到我們將塊與方法一起傳遞。 當在哈希對象中找不到鍵時,該塊已經運行,并且其值已返回。

翻譯自: https://www.includehelp.com/ruby/hash-fetch-method-with-example.aspx

ruby hash方法

總結

以上是生活随笔為你收集整理的ruby hash方法_Hash.fetch()方法以及Ruby中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。