Ruby 哈希(Hash)

哈希(Hash)是类似 "key" => "value" 这样的键值对集合。 哈希类似于一个数组,只不过它的索引不局限于使用数字。 Hash 的索引(或者叫"键")几乎可以是任何对象。Hash 虽然和数组类似,但却有一个很重要的区别: Hash 的元素没有特定的顺序。 如果顺序很重要的话就要使用数组了。

1、创建哈希

与数组一样,有各种不同的方式来创建哈希。您可以通过 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]}"

a. 哈希对象的增删改查 b. 哈希对象的转化能力 b. 哈希对象的迭代能力 c. 未知能力

2、哈希内置方法

如果需要调用 Hash 方法,需要先实例化一个 Hash 对象。下面是创建 Hash 对象实例的方式:

Hash[[key =>|, value]* ] or Hash.new [or] Hash.new(obj) [or] Hash.new { |hash, key| block } 这将返回一个使用给定对象进行填充的新的哈希。现在,使用创建的对象,我们可以调用任意可用的方法

1)对象的增删改查

1、hash == other_hash 检查两个哈希是否具有相同的键值对个数,键值对是否相互匹配,来判断两个哈希是否相等。 2、hash.clear 从哈希中移除所有的键值对。 3、hash.empty? 检查 hash 是否为空(不包含键值对),返回 true 或 false。 4、hash[key]=value [or] hash.store(key, value) 哈希对象添加新的键值对, 将 key 值与 value 给定的键进行关联。 5、hash.delete(key) [or] hash.delete(key) { |key| block } 通过 key 从 hash 中删除键值对。如果使用了块 且未找到匹配的键值对,则返回块的结果。把它与 delete_if 进行比较 6、hash.update(other_hash) [or] hash.update(other_hash) {|key, oldval, newval| block} 返回一个新的哈希,包含 hash 和 other_hash 的内容,重写 hash 中与 other_hash 带有重复键的键值对。 7、hash.replace(other_hash) 把 hash 的内容替换为 other_hash 的内容。 8、hash.key?(key) [or] hash.include?(key) [or] hash.member?(key) 哈希对象查询当前所有key中,是否包含指定的key,返回true,false 9、hash.value?(value) 哈希对象查询当前所有value中,是否包含指定的value,返回true,false。 10、hash[key] [or] hash.values_at(keys, ...) 哈希对象通过键索引,返回指定索引的value值。后者以数组形式返回。 11、hash.default = obj 哈希对象设置自己的默认值,通过设置的对象才可查询默认值。 12、hash.default(key = nil) 返回 hash 的默认值,如果未通过 default= 进行设置,则返回 nil。 13、hash.keys 哈希对象返回当前所有的key值,以数组的形式 14、hash.values 哈希对象返回当前所有的value值,以数组的形式。

2)对象的转化能力

1、hash.shift 从 hash 中移除一个键值对,并把该键值对作为二元素数组返回。 2、hash.size 以整数形式返回 hash 的 size 或 length。 3、hash.sort 把 hash 转换为一个包含键值对数组的二维数组,然后进行排序。 4、hash.to_a 从 hash 中创建一个二维数组。每个键值对转换为一个数组,所有这些数组都存储在一个数组中。 5、hash.to_hash 返回 hash(self)。 6、hash.to_s 把 hash 转换为一个数组,然后把该数组转换为一个字符串。 7、hash.inspect 返回哈希的打印字符串版本。 8、hash.invert 创建一个新的 hash,倒置 hash 中的 keys 和 values。也就是说,在新的哈希中,hash 中的键将变成值,值将变成键。 9、hash.length 以整数形式返回 hash 的大小或长度。 10、hash.rehash 基于每个 key 的当前值重新建立 hash。如果插入后值发生了改变,该方法会重新索引 hash。

3)对象的迭代能力

1、hash.each { |key,value| block } 遍历 hash,将每个 key 和 value 调用一次 block,传递 key-value 作为一个二元素数组。 2、hash.each_key { |key| block } 遍历 hash,将每个 key 调用一次 block,传递 key 作为参数。。 3、hash.each_value { |value| block } 遍历 hash,将每个 value 调用一次 block,传递 value 作为参数。 4、hash.select { |key, value| block } 遍历 hash,将每个 key 和 value 调用一次 block,传递 key-value 作为一个二元素数组。同each 5、hash.delete_if { |key,value| block } 遍历hash,将每个 key 和 value 调用一次block ,返回 true 的将从 hash 中删除键值对。 6、hash.reject { |key, value| block } 遍历hash,将每个 key 和 value 调用一次block ,返回 true 的将从一个拷贝哈希中删除键值对。 7、hash.reject! { |key, value| block } 相等于 delete_if, 但是如果没有修改,返回 nil。 8、hash.fetch(key [, default] ) [or] hash.fetch(key) { | key | block } 通过给定的 key 从 hash 返回值。如果未找到 key,且未提供其他参数,则抛出 IndexError 异常; 如果给出了 default,则返回 default;如果指定了可选的 block,则返回 block 的结果。 9、hash.merge(other_hash) [or] hash.merge(other_hash) { |key, oldval, newval| block } 返回一个新的哈希,包含 hash 和 other_hash 的内容,重写 hash 中与 other_hash 带有重复键的键值对。 10、hash.merge!(other_hash) [or] hash.merge!(other_hash) { |key, oldval, newval| block } 与 merge 相同,但实际上 hash 发生了变化。 11、hash.default_proc 如果 hash 通过块来创建,则返回块。