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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

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

ruby hash方法

Hash.key?(obj)方法 (Hash.key?(obj) Method)

In this article, we will study about Hash.key?(obj) Method. The working of the method can't be assumed because of it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我們將研究Hash.key?(obj)方法 。 由于該方法的名稱完全不同,因此無法進行假設。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。

Method description:

方法說明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.key?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be a normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 Hash.key?()方法用于檢查鍵(鍵值)是否為特定Hash實例的一部分,并且該Hash實例應為普通Hash實例。 它將搜索整個哈希,并根據其搜索結果。 讓我們來看一下語法,并演示該方法的程序代碼。

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.

如果您正在考慮它將返回什么,那么讓我告訴您, 它將返回一個布爾值 。 如果在哈希表中找到密鑰,則返回值將為true;如果找不到哈希表實例的一部分,則返回值為false。

Syntax:

句法:

Hash_instance.key?(obj)

Argument(s) required:

所需參數:

This method only takes one parameter and that argument is nothing but the key whose presence we want to check.

此方法僅使用一個參數,而該參數不過是我們要檢查其存在性的鍵。

Example 1:

范例1:

=beginRuby program to demonstrate Hash.key? method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}puts "Hash.key? implementation:"puts "Enter the Key you want to search: " ky = gets.chompif (hsh.key?(ky))puts "Key found successfully" elseputs "Key not found!" end

Output

輸出量

Hash.key? implementation: Enter the Key you want to search: colors Key found successfully

Explanation:

說明:

In the above code, you can observe that we are invoking the Hash.key?() method on the normal Hash instance. It has returned true when it found the presence of key in the Hash object which is entered by the user.

在上面的代碼中,您可以觀察到我們在普通的Hash實例上調用Hash.key?()方法 。 當它在用戶輸入的哈希對象中發現密鑰存在時,它返回true。

Example 2:

范例2:

=beginRuby program to demonstrate Hash.key? method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"} hsh1 = {"cars" => "800","bike" => "pulsar", "phone" => "A50"} hsh2 = {"one"=> hsh, "two" => hsh1}puts "Hash.key? implementation:"puts "Enter the Key you want to search: " ky = gets.chompif (hsh2.key?(ky))puts "Key found successfully" elseputs "Key not found!" end

Output

輸出量

Hash.key? implementation: Enter the Key you want to search: colors Key not found!

Explanation:

說明:

In the above code, you can verify that Hash.key?() method does not work upon Hash instance which is the collection of multiple Hash instances. It will return false even if the object is a part of the Hash instance.

在上面的代碼中,您可以驗證Hash.key?()方法不適用于Hash實例,該實例是多??個Hash實例的集合。 即使對象是Hash實例的一部分,它也會返回false。

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

ruby hash方法

總結

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

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