Ruby 哈希(Hash)

哈希(Hash)是類似 "key" => "value" 這樣的鍵值對(duì)集合。哈希類似于一個(gè)數(shù)組,只不過(guò)它的索引不局限于使用數(shù)字。

Hash 的索引(或者叫"鍵")幾乎可以是任何對(duì)象。

Hash 雖然和數(shù)組類似,但卻有一個(gè)很重要的區(qū)別:Hash 的元素沒(méi)有特定的順序。 如果順序很重要的話就要使用數(shù)組了。

創(chuàng)建哈希

與數(shù)組一樣,有各種不同的方式來(lái)創(chuàng)建哈希。您可以通過(guò) new 類方法創(chuàng)建一個(gè)空的哈希:

months = Hash.new

您也可以使用 new 創(chuàng)建帶有默認(rèn)值的哈希,不帶默認(rèn)值的哈希是 nil

months = Hash.new( "month" )
 
或
 
months = Hash.new "month"

當(dāng)您訪問(wèn)帶有默認(rèn)值的哈希中的任意鍵時(shí),如果鍵或值不存在,訪問(wèn)哈希將返回默認(rèn)值:

在線示例

#!/usr/bin/ruby
 
months = Hash.new( "month" )
 
puts "#{months[0]}"
puts "#{months[72]}"

以上示例運(yùn)行輸出結(jié)果為:

month
month

在線示例

#!/usr/bin/ruby
 
H = Hash["a" => 100, "b" => 200]
 
puts "#{H['a']}"
puts "#{H['b']}"

以上示例運(yùn)行輸出結(jié)果為:

100
200

您可以使用任何的 Ruby 對(duì)象作為鍵或值,甚至可以使用數(shù)組,如下示例所示:

[1,"jan"] => "January"

哈希內(nèi)置方法

如果需要調(diào)用 Hash 方法,需要先示例化一個(gè) Hash 對(duì)象。下面是創(chuàng)建 Hash 對(duì)象示例的方式:

Hash[[key =>|, value]* ] or
 
Hash.new [or] Hash.new(obj) [or]
 
Hash.new { |hash, key| block }

這將返回一個(gè)使用給定對(duì)象進(jìn)行填充的新的哈?!,F(xiàn)在,使用創(chuàng)建的對(duì)象,我們可以調(diào)用任意可用的方法。例如:

在線示例

#!/usr/bin/ruby
 
$, = ", "
months = Hash.new( "month" )
 
months = {"1" => "January", "2" => "February"}
 
keys = months.keys
 
puts "#{keys}"

以上示例運(yùn)行輸出結(jié)果為:

["1", "2"]

下面是公共的哈希方法(假設(shè) hash 是一個(gè) Hash 對(duì)象):

序號(hào)方法 & 描述
1hash == other_hash
檢查兩個(gè)哈希是否具有相同的鍵值對(duì)個(gè)數(shù),鍵值對(duì)是否相互匹配,來(lái)判斷兩個(gè)哈希是否相等。
2hash[key]
使用鍵,從哈希引用值。如果未找到鍵,則返回默認(rèn)值。
3hash[key]=value
value 給定的值與 key 給定的鍵進(jìn)行關(guān)聯(lián)。
4hash.clear
從哈希中移除所有的鍵值對(duì)。
5hash.default(key = nil)
返回 hash 的默認(rèn)值,如果未通過(guò) default= 進(jìn)行設(shè)置,則返回 nil。(如果鍵在 hash 中不存在,則 [] 返回一個(gè)默認(rèn)值。)
6hash.default = obj
hash 設(shè)置默認(rèn)值。
7hash.default_proc
如果 hash 通過(guò)塊來(lái)創(chuàng)建,則返回塊。
8hash.delete(key) [or]
array.delete(key) { |key| block }

通過(guò) keyhash 中刪除鍵值對(duì)。如果使用了塊 且未找到匹配的鍵值對(duì),則返回塊的結(jié)果。把它與 delete_if 進(jìn)行比較。
9hash.delete_if { |key,value| block }
block 為 true 的每個(gè)塊,從 hash 中刪除鍵值對(duì)。
10hash.each { |key,value| block }
遍歷 hash,為每個(gè) key 調(diào)用一次 block,傳遞 key-value 作為一個(gè)二元素?cái)?shù)組。
11hash.each_key { |key| block }
遍歷 hash,為每個(gè) key 調(diào)用一次 block,傳遞 key 作為參數(shù)。
12hash.each_key { |key_value_array| block }
遍歷 hash,為每個(gè) key 調(diào)用一次 block,傳遞 keyvalue 作為參數(shù)。
13hash.each_value { |value| block }
遍歷 hash,為每個(gè) key 調(diào)用一次 block,傳遞 value 作為參數(shù)。
14hash.empty?
檢查 hash 是否為空(不包含鍵值對(duì)),返回 truefalse。
15hash.fetch(key [, default] ) [or]
hash.fetch(key) { | key | block }

通過(guò)給定的 keyhash 返回值。如果未找到 key,且未提供其他參數(shù),則拋出 IndexError 異常;如果給出了 default,則返回 default;如果指定了可選的 block,則返回 block 的結(jié)果。
16hash.has_key?(key) [or] hash.include?(key) [or]
hash.key?(key) [or] hash.member?(key)

檢查給定的 key 是否存在于哈希中,返回 truefalse。
17hash.has_value?(value)
檢查哈希是否包含給定的 value
18hash.index(value)
為給定的 value 返回哈希中的 key,如果未找到匹配值則返回 nil。
19hash.indexes(keys)
返回一個(gè)新的數(shù)組,由給定的鍵的值組成。找不到的鍵將插入默認(rèn)值。該方法已被廢棄,請(qǐng)使用 select。
20hash.indices(keys)
返回一個(gè)新的數(shù)組,由給定的鍵的值組成。找不到的鍵將插入默認(rèn)值。該方法已被廢棄,請(qǐng)使用 select。
21hash.inspect
返回哈希的打印字符串版本。
22hash.invert
創(chuàng)建一個(gè)新的 hash,倒置 hash 中的 keysvalues。也就是說(shuō),在新的哈希中,hash 中的鍵將變成值,值將變成鍵。
23hash.keys
創(chuàng)建一個(gè)新的數(shù)組,帶有 hash 中的鍵。
24hash.length
以整數(shù)形式返回 hash 的大小或長(zhǎng)度。
25hash.merge(other_hash) [or]
hash.merge(other_hash) { |key, oldval, newval| block }

返回一個(gè)新的哈希,包含 hashother_hash 的內(nèi)容,重寫(xiě) hash 中與 other_hash 帶有重復(fù)鍵的鍵值對(duì)。
26hash.merge!(other_hash) [or]
hash.merge!(other_hash) { |key, oldval, newval| block }

與 merge 相同,但實(shí)際上 hash 發(fā)生了變化。
27hash.rehash
基于每個(gè) key 的當(dāng)前值重新建立 hash。如果插入后值發(fā)生了改變,該方法會(huì)重新索引 hash。
28hash.reject { |key, value| block }
類似 delete_if, 但作用在一個(gè)拷貝的哈希上。相等于 hsh.dup.delete_if。
29hash.reject! { |key, value| block }
相等于 delete_if, 但是如果沒(méi)有修改,返回 nil。
30hash.replace(other_hash)
hash 的內(nèi)容替換為 other_hash 的內(nèi)容。
31hash.select { |key, value| block }
返回一個(gè)新的數(shù)組,由 block 返回 truehash 中的鍵值對(duì)組成。
32hash.shift
hash 中移除一個(gè)鍵值對(duì),并把該鍵值對(duì)作為二元素?cái)?shù)組返回。
33hash.size
以整數(shù)形式返回 hashsize 或 length。
34hash.sort
hash 轉(zhuǎn)換為一個(gè)包含鍵值對(duì)數(shù)組的二維數(shù)組,然后進(jìn)行排序。
35hash.store(key, value)
存儲(chǔ) hash 中的一個(gè)鍵值對(duì)。
36hash.to_a
從 hash 中創(chuàng)建一個(gè)二維數(shù)組。每個(gè)鍵值對(duì)轉(zhuǎn)換為一個(gè)數(shù)組,所有這些數(shù)組都存儲(chǔ)在一個(gè)數(shù)組中。
37hash.to_hash
返回 hash(self)。
38hash.to_s
hash 轉(zhuǎn)換為一個(gè)數(shù)組,然后把該數(shù)組轉(zhuǎn)換為一個(gè)字符串。
39hash.update(other_hash) [or]
hash.update(other_hash) {|key, oldval, newval| block}

返回一個(gè)新的哈希,包含 hashother_hash 的內(nèi)容,重寫(xiě) hash 中與 other_hash 帶有重復(fù)鍵的鍵值對(duì)。
40hash.value?(value)
檢查 hash 是否包含給定的 value。
41hash.values
返回一個(gè)新的數(shù)組,包含 hash 的所有值。
42hash.values_at(obj, ...)
返回一個(gè)新的數(shù)組,包含 hash 中與給定的鍵相關(guān)的值。
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清