国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Rubyのチュートリアル / Ruby 哈希(Hash)

Ruby 哈希(Hash)

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

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

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

創(chuàng)建哈希

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

months = Hash.new

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

months = Hash.new( "month" )

或

months = Hash.new "month"

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

#!/usr/bin/ruby

months = Hash.new( "month" )

puts "#{months[0]}"
puts "#{months[72]}"


以上實例運行輸出結(jié)果為:

month
month
#!/usr/bin/ruby

H = Hash["a" => 100, "b" => 200]

puts "#{H['a']}"
puts "#{H['b']}"


以上實例運行輸出結(jié)果為:

100
200

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

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

哈希內(nèi)置方法

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

Hash[[key =>|, value]* ] or

Hash.new [or] Hash.new(obj) [or]

Hash.new { |hash, key| block }

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

#!/usr/bin/ruby

$, = ", "
months = Hash.new( "month" )

months = {"1" => "January", "2" => "February"}

keys = months.keys

puts "#{keys}"


以上實例運行輸出結(jié)果為:

["1", "2"]

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

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

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

通過給定的 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)
返回一個新的數(shù)組,由給定的鍵的值組成。找不到的鍵將插入默認(rèn)值。該方法已被廢棄,請使用 select。
20hash.indices(keys)
返回一個新的數(shù)組,由給定的鍵的值組成。找不到的鍵將插入默認(rèn)值。該方法已被廢棄,請使用 select。
21hash.inspect
返回哈希的打印字符串版本。
22hash.invert
創(chuàng)建一個新的 hash,倒置 hash 中的 keysvalues。也就是說,在新的哈希中,hash 中的鍵將變成值,值將變成鍵。
23hash.keys
創(chuàng)建一個新的數(shù)組,帶有 hash 中的鍵。/td>
24hash.length
以整數(shù)形式返回 hash 的大小或長度。
25hash.merge(other_hash) [or]
hash.merge(other_hash) { |key, oldval, newval| block }

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

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

返回一個新的哈希,包含 hashother_hash 的內(nèi)容,重寫 hash 中與 other_hash 帶有重復(fù)鍵的鍵值對。
40hash.value?(value)
檢查 hash 是否包含給定的 value。
41hash.values
返回一個新的數(shù)組,包含 hash 的所有值。
42hash.values_at(obj, ...)
返回一個新的數(shù)組,包含 hash 中與給定的鍵相關(guān)的值。