Hash类说明
函数名称 |
说明 |
示例 |
== |
判断两个 Hash 是否相等 |
h1 = {"a" => 1, "c" => 2} h2 = { "a" => 1, "c" => 2, 7 => 35 } h1 == h2 » false |
[ ] |
返回指定键值对应的对象 |
h = { "a" => 100, "b" => 200 } h["a"] » 100 h["c"] » nil |
[ ]= |
向 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 » {} |
default |
返回哈希表的默认值 |
h = { "a" => 100, "b" => 200 } h.default = "Go fish" h["a"] » 100 h["z"] » "Go fish" |
delete |
从词典中删除和键值相符的记录 |
h = { "a" => 100, "b" => 200 } h.delete("a") » 100 h.delete("z") » nil h.delete("z") { |el| "#{el} not found" } » "z not found" |
delete_if |
通过过程块来删除特定键值的记录 |
h = { "a" => 100, "b" => 200 } h.delete_if {|key, value| key >= "b" } » {"a"=>100} |
each |
Hash 表的迭代操作,对表的每一个词对 进行迭代操作 |
h = { "a" => 100} h.each {|key, value| print key, " is ",value,"\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? » true |
fetch |
如果能找到键值为 key 的 hash 值,则 返回 Hash 值; 如果找不到,则返回默认值或指定值; 如果默认值和指定值都找不到,抛异常 |
h = { "a" => 100, "b" => 200 } h.fetch("a") » 100 h.fetch("z", "go fish") » "go fish" h.fetch("k") »in `fetch': key not found (IndexError) |
has_key? |
判断是否存在相符的 key 值 |
h = { "a" => 100, "b" => 200 } h.has_key?("a") » true h.has_key?("z") » false |
key? |
h. key?("z") » false |
|
include? |
h.include?("b") » true |
|
has_value? |
判断是否存在相符的 value 值 |
h = { "a" => 100, "b" => 200 } h.has_value?(100) » true h.has_value?(999) » false |
value? |
h.value?(100) » true h.value?(999) » false |
|
index |
返回给定值的键值,未找到返 nil |
h = { "a" => 100, "b" => 200 } h.index(200) » "b" h.index(999) » nil |
indexes |
返回一系列给定的键值对应值组成的数 组 |
h = { "a" =>100, "b" =>200, "c" =>300 } h.indexes("a", "c") » [100,300] h.indexes("a","z") » [100,nil] |
indices |
|
h.indexes("a", "c") » [100,300] h.indexes("a", "z") » [100,nil] |
invert |
将元素值和索引互换,返回变换后的哈 希表. 注意:若原哈希表中若干不同的索引对 应相同的元素值时,其变换结果将无法 预测. |
h = { "n" => 100,"m" => 100,"y" => 300} h.invert » {300=>"y",100=>"n"} |
keys |
返回一个包含所有 key 的数组. |
h = { "a" => 100, "b" => 200 } h.keys » ["a", "b"] |
values |
返回一个包含所有 vakue 的数组. |
h = { "a" => 100,"b" => 200,"c" => 300 } h.values » [100, 200, 300] |
length |
返回词典中元素的个数 |
h = { "d" =>100, "a"=> 200, "v" => 300} h.length » 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]] |
to_s |
把 Hash 表转换为字符串 |
h = { "c" => 300, "a" => 100, "d" => 400} h.to_s » "a100c300d400" |
update |
用一张 Hash 表去更新另外张 Hash 表 |
h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.update(h2) » {"a"=>100, "b"=>254, "c"=>300} |