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:

=begin
Ruby 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.chomp
if (hsh.key?(ky))
puts "Key found successfully"
else
puts "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:

=begin
Ruby 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.chomp
if (hsh2.key?(ky))
puts "Key found successfully"
else
puts "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方法

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

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

相關文章

python迭代器與生成器答案_史上最全 Python 迭代器與生成器

原標題:史上最全 Python 迭代器與生成器作者:浪子燕青鏈接:http://www.langzi.fun/迭代器與生成器.html迭代器與可迭代對象概念迭代器:是訪問數據集合內元素的一種方式,一般用來遍歷數據,但是他不能像列表一…

[性能測試] LoadRunner結果分析 – TPS

本文轉載自:http://www.tuicool.com/articles/6z6vuy針對吞吐率和 TPS 的關系,這個在結果分析中如何使用,就個人經驗和朋友討論后,提出如下建議指導,歡迎同僚指正。相關定義響應時間 網絡響應時間 應用程序響應時間響…

密碼學電子書_密碼學中的電子密碼書(ECB)

密碼學電子書This Electronic Code Book (ECB) is cryptography as a mode of operation for a block cipher, with the characters the main things that every feasible block of plaintext or an original text has a corresponding characteristic of ciphertext value and…

tsql是mysql中的嗎_Mysql中的sql是如何執行的

MySQL中的SQL是如何執行的MySQL是典型的C/S架構,也就是Client/Server架構,服務器端程序使用的mysqld.整體的MySQL流程如下圖所示:MySQL是有三層組成:連接層: 負責客戶端與服務器端建立連接,客戶端發送SQL至服務端;SQL層: 對SQL語句進行查詢處理;存儲引擎層: 與數據庫文件打交道…

軟件質量特性測試

針對軟件質量特性進行測試,可以避免重大漏測,一般人我不告訴他。《軟件工程—產品質量》(GB/T 16260-2006)中規定對軟件的每個質量特性與子特性都有定義:一、功能性:是指當軟件在指定條件下使用&#xff0c…

PHP array_pop()函數與示例

PHP array_pop()函數 (PHP array_pop() function) array_pop() function is used to delete/pop last element from the array. array_pop()函數用于從數組中刪除/彈出最后一個元素。 Syntax: 句法: array_pop(array);Here, array is the input array, function w…

網站關停就沒事了?5100萬賬戶文件被盜

曾經是美國三大音樂視頻文件共享軟件之一的imesh,意外倒閉。而更意外的是,就在近日,imesh這款已經倒閉的軟件,5100萬賬戶開始在暗網被黑客拍賣。 Imesh這款軟件是美國紐約的老牌音樂視頻分享軟件之一,早在2000年前便已…

數據庫表設計索引外鍵設計_關于索引的設計決策 數據庫管理系統

數據庫表設計索引外鍵設計Introduction: 介紹: The attributes whose values are required inequality or range conditions and those that are keys or that participate in join conditions require access paths. 其值為必需的不等式或范圍條件的屬性以及作為鍵…

接口測試從零開始系列_mock技術使用

1、什么情況下會使用mock技術 (1)需要將當前被測單元和其依賴模塊獨立開來,構造一個獨立的測試環境,不關注被測單元的依賴對象,只關注被測單元的功能邏輯 ----------比如被測代碼中需要依賴第三方接口返回值進行邏輯處…

amie 規則挖掘_AMIE的完整形式是什么?

amie 規則挖掘AMIE:工程師協會的準會員 (AMIE: Associate Member of the Institution of Engineers) AMIE is an abbreviation of Associate Member of the Institution of Engineers. The Institution of Engineers India Limited (IEIL) provides this profession…

java 馬克思_單鏈表-Java

public class SinglyListNode {int val;SinglyListNode next;SinglyListNode() {}SinglyListNode(int x) {this.val x;}}/*執行用時:12 ms, 在所有 Java 提交中擊敗了66.93%的用戶內存消耗:39.5 MB, 在所有 Java 提交中擊敗了5.06%的用戶*/class MyLink…

python的pass語句_Python | 演示pass語句的示例

python的pass語句python中的pass語句 (pass statement in python) "pass" is a type of null operation or null statement, when it executes nothing happens. It is used when you want do not want to write any code/statement to execute but syntactically a …

HDS:聚焦未來的投資“凍結”

一家日本IT網站報道的有關HDS凍結對高端存儲產品的投資一事引發眾議。讓人陷入疑惑的這次聲明就是,HDS認為單純的陣列產品并非企業存儲的未來。 6月1日,IT Pro Nikkei網站發布了一篇報道,內容援引HDS一份表示將凍結高端存儲業務的簡報。這引發…

java js對象轉字符串數組_JS數組轉字符串(3種方法)【轉】

JavaScript 允許數組與字符串之間相互轉換。其中 Array 方法對象定義了 3 個方法,可以把數組轉換為字符串,如表所示。數組方法說明toString()將數組轉換成一個字符串toLocalString()把數組轉換成本地約定的字符串join()將數組元素連接起來以構建一個字符…

中美共建大數據創新研究中心

由貴陽市人民政府、工信部電子一所、美國加州大學伯克利分校合作共建的貴州伯克利大數據創新研究中心日前在貴陽揭牌。 據了解,貴州伯克利大數據創新研究中心將分兩階段建設。第一階段,2016年9月份至2017年底,將重點完成“學齡兒童大數據分析…

Python中的__init__和self是做什么的?

The __init__ and self are two keywords in python, which performs a vital role in the application. __init__和self是python中的兩個關鍵字,在應用程序中起著至關重要的作用。 To begin with, it is important to understand the concept of class and object…

Palo Alto Networks漏洞防護擴展至云端

中國北京,2016年4月12日 –下一代安全企業Palo Alto Networks?(紐交所代碼:PANW)近日宣布進一步增強其下一代安全平臺,擴展漏洞防護能力,以滿足那些依賴云環境和SaaS應用的業務對安全的需求。 企業機構需要變得更加靈活和有競爭力…

java 嵌套調用_Java嵌套類的使用

嵌套類是指被定義在另一個類內部的類,它為外部類提供服務。嵌套類分四種:靜態成員類、非靜態成員類、匿名類和局部類。一、靜態成員類與非靜態成員類的區別?在什么情況下可以用靜態成員類?我們知道在類的設計中,為了避…

c語言getenv函數_getenv()函數與C ++中的示例

c語言getenv函數C getenv()函數 (C getenv() function) getenv() function is a library function of cstdlib header. It is used to get the environment string. It accepts a parameter which is an environment variable name (platform dependent, it may either case s…

isless()函數與C ++中的示例

C isless()函數 (C isless() function) isless() function is a library function of cmath header, it is used to check whether the given first value is less than the second value. It accepts two values (float, double or long double) and returns 1 if the first …