ruby and watir学习之_Hash 类

http://www.cnblogs.com/zhangfei/archive/2010/07/28/1786929.html

 

 

ash类说明

函数名称

说明

示例

 

 

= =

 

判断两个 Hash 是否相等

h 1 = {"a" => 1, "c" => 2}

h 2 = { "a" => 1, "c" => 2, 7 => 35 }

h 1 == h2         » f alse

 

 

[ ]

 

返回指定键值对应的对象

h = { "a" => 100, "b" => 200 }

h [ " a " ]   » 100 h["c"]   » n i l

 

 

 

[ ]=

 

 

向 Hash 添加记录

h = { "a" => 100, "b" => 200 }

h [ " a " ] = 9 h["c"] = 4

h   » {"a"=>9, "b"=>200, "c"=>4}

 

clear

清空哈希表的内容. 返回 self.

h = { "a" => 100, "b" => 200 }

h . clear         » {}

 

 

 

d efault

 

 

返回哈希表的默认值

h = { "a" => 100, "b" => 200 }

h . d efault = "Go fish" h["a"]   » 100

h [ " z " ]   » " G o fish"

 

 

 

d elete

 

 

 

从词典中删除和键值相符的记录

h = { "a" => 100, "b" => 200 } h.delete("a")                    » 100 h.delete("z")                    » n i l h.delete("z") { |el| "#{el} not found" }

» " z not found"

 

 

d elete_if

 

通过过程块来删除特定键值的记录

h = { "a" => 100, "b" => 200 }

h . d elete_if {|key, value| key >= "b" }

» {"a"=>100}

 

 

each

 

Hash 表的迭代操作,对表的每一个词对 进行迭代操作

h = { "a" => 100}

h . each  {|key,  value|  print  key,  "  is

" , v alue,"\n"}               » a is 100

 

each_key

对表的每一个键对进行迭代操作

h = { "a" => 100, "b" => 200 }

h . each_key {|key| print key }        »    ab

 

 

each_value

 

针对 value 进行迭代操作

h = { "a" => 100, "b" => 200 }

h . each_value {|value| print value }

»    100200

empty?

判断哈希表是否为空,空则返回 true

{}.empty?       » t rue

 

 

 

f etch

 

如果能找到键值为 key 的 hash 值,则 返回 Hash 值; 如果找不到,则返回默认值或指定值; 如果默认值和指定值都找不到,抛异常

h = { "a" => 100, "b" => 200 } h.fetch("a")                   » 100 h.fetch("z", "go fish")  » "g o fish" h.fetch("k")

» i n `fetch': key not found (IndexError)

 

 

h as_key?

 

 

 

判断是否存在相符的 key 值

h = { "a" => 100, "b" => 200 }

h . h as_key?("a")   » t rue h.has_key?("z")   » f alse

key

h . key?("z")     » f alse

i n clude?

h .i n clude?("b")    » t rue

 

 

h as_value?

 

 

 

判断是否存在相符的 value 值

h = { "a" => 100, "b" => 200 }

h . h as_value?(100)  » t r u e h.has_value?(999)  » f alse

 

v alue?

h . v alue?(100)  » t rue h.value?(999)  » f alse

 

 

i nd ex

 

返回给定值的键值,未找到返 nil

h = { "a" => 100, "b" => 200 }

h .i nd e x(200)  » " b " h.index(999)  » n i l

 

 

i nd exes

 

返回一系列给定的键值对应值组成的数 组

h = { "a" =>100, "b" =>200, "c" =>300 }

h .i nd e xes("a", "c")         » [ 100,300]

h .i nd e xes("a","z")           » [ 100,nil]

 

i nd i ces

 

h .i nd e xes("a", "c")         » [ 100,300]

h.indexes("a", "z")      » [100,nil]

 

 

 

i n v ert

将元素值和索引互换,返回变换后的哈

希表. 注意: 若原哈希表中若干不同的索引对 应相同的元素值时, 其变换结果将无法 预测.

 

 

 

h = { "n" => 100,"m" => 100,"y" => 300}

h .i n v ert       » {300=>"y",100=>"n"}

 

keys

返回一个包含所有 key 的数组.

h = { "a" => 100, "b" => 200 }

h . keys                 » [ " a", "b"]

 

v alues

返回一个包含所有 vakue 的数组.

h = { "a" => 100,"b" => 200,"c" => 300 }

h . v alues          » [ 100, 200, 300]

l ength

 

返回词典中元素的个数

h = { "d" =>100, "a"=> 200, "v" => 300}

h .l ength          » 3 h. size             » 3

 

size

 

 

 

 

 

rehash

 

 

重新计算索引对应的哈希表值。 当与索引对应的哈希表值发生变化时, 若不使用该方法来重新计算的话,将无 法取出与索引对应的哈希表值。

a = [ "a", "b" ]

c = [ "c", "d" ]

h = { a => 100, c => 300 }

a[0] = "z" h.rehash

» {["z", "b"]=>100, ["c", "d"]=>300}

h [ a]   » 100

 

 

replace

以另外一张 Hash 表的内容来替换当前

Hash 表的内容

h = {"a" =>100, "b" =>200 }

h . replace({ "c" => 300, "d" => 400 })

» {"c"=>300, "d"=>400}

 

 

shift

 

删除一个哈希表元素后 再以[key,value]数组的形式将其返回

h = { 1 => "a", 2 => "b", 3 => "c" }

h . shift   » [ 1, "a"]

h   » {2=>"b", 3=>"c"}

 

 

 

sort

 

对 Hash 进行排序 按键值从小到大排序

h = { "b" => 30, "a" => 20, "c" => 10  } h.sort   » [[ " a " , 20], ["b", 30], ["c", 10]] h.sort {|a,b| a[1]<=>b[1]}

» [[ " c", 10], ["a", 20], ["b", 30]]

 

to_a

把 Hash 表转换为数组

数组按 Hash 表的键值从小到大排序

h = {"c" =>300,"a" =>100,"d" =>400}

»    [[ " a", 100], ["c", 300], ["d", 400]]

 

t o_ s

把 Hash 表转换为字符串

h = { "c" => 300, "a" => 100, "d" => 400}

h .t o_ s          » " a100c300d400"

 

 

 

u p d ate

 

 

用一张 Hash 表去更新另外张 Hash 表

h 1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.update(h2)

» {"a"=>100, "b"=>254, "c"=>300}

你可能感兴趣的:(watir)