Ruby 哈希(Hash)
哈希(Hash)是類似 "key" => "value" 這樣的鍵值對集合。哈希類似於一個數組,只不過它的索引不局限於使用數字。
Hash 的索引(或者叫"鍵")幾乎可以是任何對象。
Hash 雖然和數組類似,但卻有一個很重要的區別:Hash 的元素沒有特定的順序。 如果順序很重要的話就要使用數組了。
創建哈希
與數組一樣,有各種不同的方式來創建哈希。您可以通過 new 類方法創建一個空的哈希:
months = Hash.new
您也可以使用 new 創建帶有默認值的哈希,不帶默認值的哈希是 nil:
months = Hash.new( "month" )
或
months = Hash.new "month"
當您訪問帶有默認值的哈希中的任意鍵時,如果鍵或值不存在,訪問哈希將返回默認值:
實例
#!/usr/bin/ruby
months = Hash.new( "month" )
puts "#{months[0]}"
puts "#{months[72]}"
以上實例運行輸出結果為:
month month
實例
#!/usr/bin/ruby
H = Hash["a" => 100, "b" => 200]
puts "#{H['a']}"
puts "#{H['b']}"
以上實例運行輸出結果為:
100 200
您可以使用任何的 Ruby 對象作為鍵或值,甚至可以使用數組,如下實例所示:
[1,"jan"] => "January"
哈希內置方法
如果需要調用 Hash 方法,需要先實例化一個 Hash 對象。下麵是創建 Hash 對象實例的方式:
Hash[[key =>|, value]* ] or
Hash.new [or] Hash.new(obj) [or]
Hash.new { |hash, key| block }
這將返回一個使用給定對象進行填充的新的哈希。現在,使用創建的對象,我們可以調用任意可用的方法。例如:
實例
#!/usr/bin/ruby
$, = ", "
months = Hash.new( "month" )
months = {"1" => "January", "2" => "February"}
keys = months.keys
puts "#{keys}"
以上實例運行輸出結果為:
["1", "2"]
下麵是公共的哈希方法(假設 hash 是一個 Hash 對象):
序號 | 方法 & 描述 |
---|---|
1 | hash == other_hash 檢查兩個哈希是否具有相同的鍵值對個數,鍵值對是否相互匹配,來判斷兩個哈希是否相等。 |
2 | hash.[key] 使用鍵,從哈希引用值。如果未找到鍵,則返回默認值。 |
3 | hash.[key]=value 把 value 給定的值與 key 給定的鍵進行關聯。 |
4 | hash.clear 從哈希中移除所有的鍵值對。 |
5 | hash.default(key = nil) 返回 hash 的默認值,如果未通過 default= 進行設置,則返回 nil。(如果鍵在 hash 中不存在,則 [] 返回一個默認值。) |
6 | hash.default = obj 為 hash 設置默認值。 |
7 | hash.default_proc 如果 hash 通過塊來創建,則返回塊。 |
8 | hash.delete(key) [or] array.delete(key) { |key| block } 通過 key 從 hash 中刪除鍵值對。如果使用了塊 且未找到匹配的鍵值對,則返回塊的結果。把它與 delete_if 進行比較。 |
9 | hash.delete_if { |key,value| block } block 為 true 的每個塊,從 hash 中刪除鍵值對。 |
10 | hash.each { |key,value| block } 遍曆 hash,為每個 key 調用一次 block,傳遞 key-value 作為一個二元素數組。 |
11 | hash.each_key { |key| block } 遍曆 hash,為每個 key 調用一次 block,傳遞 key 作為參數。 |
12 | hash.each_key { |key_value_array| block } 遍曆 hash,為每個 key 調用一次 block,傳遞 key 和 value 作為參數。 |
13 | hash.each_value { |value| block } 遍曆 hash,為每個 key 調用一次 block,傳遞 value 作為參數。 |
14 | hash.empty? 檢查 hash 是否為空(不包含鍵值對),返回 true 或 false。 |
15 | hash.fetch(key [, default] ) [or] hash.fetch(key) { | key | block } 通過給定的 key 從 hash 返回值。如果未找到 key,且未提供其他參數,則拋出 IndexError 異常;如果給出了 default,則返回 default;如果指定了可選的 block,則返回 block 的結果。 |
16 | hash.has_key?(key) [or] hash.include?(key) [or] hash.key?(key) [or] hash.member?(key) 檢查給定的 key 是否存在於哈希中,返回 true 或 false。 |
17 | hash.has_value?(value) 檢查哈希是否包含給定的 value。 |
18 | hash.index(value) 為給定的 value 返回哈希中的 key,如果未找到匹配值則返回 nil。 |
19 | hash.indexes(keys) 返回一個新的數組,由給定的鍵的值組成。找不到的鍵將插入默認值。該方法已被廢棄,請使用 select。 |
20 | hash.indices(keys) 返回一個新的數組,由給定的鍵的值組成。找不到的鍵將插入默認值。該方法已被廢棄,請使用 select。 |
21 | hash.inspect 返回哈希的列印字串版本。 |
22 | hash.invert 創建一個新的 hash,倒置 hash 中的 keys 和 values。也就是說,在新的哈希中,hash 中的鍵將變成值,值將變成鍵。 |
23 | hash.keys 創建一個新的數組,帶有 hash 中的鍵。 |
24 | hash.length 以整數形式返回 hash 的大小或長度。 |
25 | hash.merge(other_hash) [or] hash.merge(other_hash) { |key, oldval, newval| block } 返回一個新的哈希,包含 hash 和 other_hash 的內容,重寫 hash 中與 other_hash 帶有重複鍵的鍵值對。 |
26 | hash.merge!(other_hash) [or] hash.merge!(other_hash) { |key, oldval, newval| block } 與 merge 相同,但實際上 hash 發生了變化。 |
27 | hash.rehash 基於每個 key 的當前值重新建立 hash。如果插入後值發生了改變,該方法會重新索引 hash。 |
28 | hash.reject { |key, value| block } 類似 delete_if, 但作用在一個拷貝的哈希上。相等於 hsh.dup.delete_if。 |
29 | hash.reject! { |key, value| block } 相等於 delete_if, 但是如果沒有修改,返回 nil。 |
30 | hash.replace(other_hash) 把 hash 的內容替換為 other_hash 的內容。 |
31 | hash.select { |key, value| block } 返回一個新的數組,由 block 返回 true 的 hash 中的鍵值對組成。 |
32 | hash.shift 從 hash 中移除一個鍵值對,並把該鍵值對作為二元素數組返回。 |
33 | hash.size 以整數形式返回 hash 的 size 或 length。 |
34 | hash.sort 把 hash 轉換為一個包含鍵值對數組的二維數組,然後進行排序。 |
35 | hash.store(key, value) 存儲 hash 中的一個鍵值對。 |
36 | hash.to_a 從 hash 中創建一個二維數組。每個鍵值對轉換為一個數組,所有這些數組都存儲在一個數組中。 |
37 | hash.to_hash 返回 hash(self)。 |
38 | hash.to_s 把 hash 轉換為一個數組,然後把該數組轉換為一個字串。 |
39 | hash.update(other_hash) [or] hash.update(other_hash) {|key, oldval, newval| block} 返回一個新的哈希,包含 hash 和 other_hash 的內容,重寫 hash 中與 other_hash 帶有重複鍵的鍵值對。 |
40 | hash.value?(value) 檢查 hash 是否包含給定的 value。 |
41 | hash.values 返回一個新的數組,包含 hash 的所有值。 |
42 | hash.values_at(obj, ...) 返回一個新的數組,包含 hash 中與給定的鍵相關的值。 |