1、哈希(Hash)
哈希也是存儲對象的一個集合发乔,哈希里面的元素是以"key" => “value”(鍵值對)這樣的形式存在的熟妓,元素是沒有順序的,哈希的鍵可以是任意對象栏尚,鍵必須的唯一的起愈,鍵通常用符號(Symbol)表示。
哈希的創(chuàng)建有兩種形式译仗,兩種形式都是一樣的抬虽,最常使用第二種:
irb(main):108:0> hash_1 = {:name => "Mary",:age => 12}
=> {:name=>"Mary", :age=>12}
irb(main):109:0> hash_2 = {name:"Mary",age:12}
=> {:name=>"Mary", :age=>12}
1
2
3
4
2、類型轉(zhuǎn)換
(1)哈希轉(zhuǎn)字符串to_s
irb(main):110:0> hash = {name:"Mary",age:12}
=> {:name=>"Mary", :age=>12}
irb(main):111:0> hash_str = hash.to_s
=> "{:name=>\"Mary\", :age=>12}"
1
2
3
4
(2)字符串轉(zhuǎn)哈希eval纵菌,字符串必須是將hash轉(zhuǎn)成字符串的那種格式阐污,不是任意字符串都可以轉(zhuǎn)成hash。通常用于從文件中讀取hash的情況咱圆。
#定義一個字符串笛辟,字符串是將hash轉(zhuǎn)成字符串的格式
irb(main):111:0> hash_str = "{:name=>\"Mary\", :age=>12}"
irb(main):112:0> eval(hash_str)
=> {:name=>"Mary", :age=>12}
#隨便定義一個字符串
irb(main):113:0> str = "hello"
=> "hello"
#eval會報錯,eval轉(zhuǎn)換的字符串必須是將hash轉(zhuǎn)成字符串的格式
irb(main):114:0> eval(str)
NameError: undefined local variable or method `hello' for main:Object
Did you mean?? help
from (irb):114:in `eval'
from (irb):114:in `eval'
from (irb):114
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3序苏、哈希操作
(1)== 判斷兩個哈希是否相等手幢,要求每個鍵值對的類型和內(nèi)容都相等,不要求順序也一樣杠览,因為哈希中的元素是沒有順序的
irb(main):116:0> h1 = {7 => 35, "c" => 2, "a" => 1}
=> {7=>35, "c"=>2, "a"=>1}
irb(main):117:0> h2 = {"c" => 2, "a" => 1, 7 => 35}
=> {"c"=>2, "a"=>1, 7=>35}
irb(main):118:0> h3 = {:c => 2, :a => 1, 7 => 35}
=> {:c=>2, :a=>1, 7=>35}
irb(main):119:0> h1 == h2
=> true
#h2弯菊、和h3中元素的區(qū)別是鍵的類型纵势,:c為符號類型踱阿、"c"為字符串類型,所以兩個hash不相等
irb(main):120:0> h2 == h3
=> false
1
2
3
4
5
6
7
8
9
10
11
(2)hash[key] 通過key取哈希的值
irb(main):121:0> hash = {7 => 35, "c" => 2, "a" => 1}
=> {7=>35, "c"=>2, "a"=>1}
irb(main):122:0> hash[7]
=> 35
irb(main):123:0> hash["c"]
=> 2
#hash中不包含key值為:c的鍵钦铁,返回nil
irb(main):124:0> hash[:c]
=> nil
1
2
3
4
5
6
7
8
9
(3)hash[key] = value 如果key值已存在软舌,則將key對應的值替換為value。如果key值不存在牛曹,則新建key => value 鍵值對
irb(main):125:0> hash = {7 => 35, "c" => 2, "a" => 1}
=> {7=>35, "c"=>2, "a"=>1}
#key為7已存在佛点,7所對應的值被替換成888
irb(main):126:0> hash[7] = 888
=> 888
#key為:c不存在,新建:c=>3鍵值對
irb(main):127:0> hash[:c] = 3
=> 3
irb(main):128:0> hash
=> {7=>888, "c"=>2, "a"=>1, :c=>3}
1
2
3
4
5
6
7
8
9
10
(4)hash.delete(key) 刪除key所在的鍵值對,并返回key所對應的value值
irb(main):129:0> hash = {7 => 35, "c" => 2, "a" => 1}
=> {7=>35, "c"=>2, "a"=>1}
irb(main):130:0> hash.delete("c")
=> 2
#hash中不包含key值為:c的鍵超营,返回nil
irb(main):131:0> hash.delete(:c)
=> nil
1
2
3
4
5
6
7
(5)hash.delete_if {| key,value | block } 刪除滿足block條件的鍵值對
irb(main):132:0> hash = {"1" => 100, "2" => 200, "3" => 300}
=> {"1"=>100, "2"=>200, "3"=>300}
#刪除掉value的值大于280的鍵值對
irb(main):133:0> hash.delete_if {|key,value| value > 280}
=> {"1"=>100, "2"=>200}
#現(xiàn)在hash中的元素
irb(main):134:0> hash
=> {"1"=>100, "2"=>200}
1
2
3
4
5
6
7
8
(6)each 遍歷哈希
hash.each {| key,value | block } 遍歷每個鍵值對
hash.each_key {| key | block } 遍歷每個鍵
hash.each_value {| value | block } 遍歷每個值
#用第二種形式定義hash鸳玩,是不是比第一種的簡單
irb(main):140:0> hash = {a:100,b:200,c:300}
=> {:a=>100, :b=>200, :c=>300}
#遍歷鍵值對,塊中有兩個變量演闭,第一個變量是鍵不跟,第二個變量是值
irb(main):141:0> hash.each {|key,value| puts "鍵為#{key},值為#{value}"}
鍵為a,值為100
鍵為b,值為200
鍵為c,值為300
=> {:a=>100, :b=>200, :c=>300}
#只遍歷鍵,塊中的變量是鍵
irb(main):142:0> hash.each_key {|key| puts "鍵為#{key}"}
鍵為a
鍵為b
鍵為c
=> {:a=>100, :b=>200, :c=>300}
#只遍歷值米碰,塊中的變量是值
irb(main):143:0> hash.each_value {|value| puts "值為#{value}"}
值為100
值為200
值為300
=> {:a=>100, :b=>200, :c=>300}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(7)hash.empty? 判斷哈希是否為空窝革,為空返回true,否則返回false
irb(main):001:0> h_1 = {}
=> {}
irb(main):002:0> h_1.blank?
=> true
1
2
3
4
(8)include 判斷哈希中是否有某個值
hash.include?(key) 判斷哈希中是否有鍵key吕座,有返回true虐译,沒有返回false
hash.has_key?(key) 與include?方法同樣用法
hash.has_value?(value) 判斷哈希中是否有值value,有返回true吴趴,沒有返回false
irb(main):003:0> hash = {a:100,b:200,c:300}
=> {:a=>100, :b=>200, :c=>300}
irb(main):004:0> hash.include?(100)
=> false
irb(main):005:0> hash.include?(:a)
=> true
irb(main):006:0> hash.has_value?(100)
=> true
1
2
3
4
5
6
7
8
(9)數(shù)組形式返回鍵漆诽、值
hash.keys 以數(shù)組的形式返回哈希所有的key
hash.values 以數(shù)組的形式返回哈希所有的value
irb(main):007:0> hash = {a:100,b:200,c:300}
=> {:a=>100, :b=>200, :c=>300}
irb(main):008:0> hash.keys
=> [:a, :b, :c]
irb(main):009:0> hash.values
=> [100, 200, 300]
1
2
3
4
5
6
(10)hash.length 哈希的長度
irb(main):010:0> hash = {a:100,b:200,c:300}
=> {:a=>100, :b=>200, :c=>300}
irb(main):011:0> hash.length
=> 3
1
2
3
4
(11)hash_1.merge(hash_2) 將hash_1和hash_2兩個哈希里面的元素合起來,返回一個新的哈希史侣,如果hash_1和hash_2兩個哈希里面有相同的key值拴泌,則key對應的值以后面的哈希(即hash_2)中的值為準。
hash_1.merge(hash_2) hash_1的值不會改變
hash_1.merge!(hash_2) hash_1的值會改變
irb(main):012:0> hash1 = {a:100,b:200}
=> {:a=>100, :b=>200}
irb(main):013:0> hash2 = {b:300,d:400}
=> {:b=>300, :d=>400}
irb(main):014:0> hash1.merge(hash2)
=> {:a=>100, :b=>300, :d=>400}
#hash1中的值沒有改變
irb(main):016:0> hash1
=> {:a=>100, :b=>200}
#merge!
irb(main):017:0> hash1.merge!(hash2)
=> {:a=>100, :b=>300, :d=>400}
#hash1中的值發(fā)生了改變
irb(main):018:0> hash1
=> {:a=>100, :b=>300, :d=>400}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(12)reject惊橱、select返回不滿足條件或者滿足條件的鍵值對
hash.reject {|key,value| block} 返回所有不滿足block條件的元素蚪腐,hash本身不會改變
hash.reject! {|key,value| block} 返回所有不滿足block條件的元素,hash本身會改變
hash.select {|key,value| block} 返回所有滿足block條件的元素税朴,hash本身不會改變
hash.select! {|key,value| block} 返回所有滿足block條件的元素回季,hash本身會改變
irb(main):019:0> hash = {a:100,b:200,c:300}
=> {:a=>100, :b=>200, :c=>300}
irb(main):020:0> hash.reject {|k,v| v > 200}
=> {:a=>100, :b=>200}
irb(main):021:0> hash.select {|k,v| v > 200}
=> {:c=>300}
————————————————
版權(quán)聲明:本文為CSDN博主「貓寧一」的原創(chuàng)文章,遵循CC 4.0 by-sa版權(quán)協(xié)議正林,轉(zhuǎn)載請附上原文出處鏈接及本聲明泡一。
原文鏈接:https://blog.csdn.net/shine_a/article/details/85276270