Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby Hash


May 12, 2021 Ruby


Table of contents


Ruby Hash

Hash is a collection of key-value pairs such as "employee" and "salary". The hashed index is done by any key of any object type, not by an integer index, which is similar to the array.

The order in which hashes are traversed by keys or values appears to be arbitrary and is usually not inserted in order. If you try to access a hash through a key that does not exist, the method returns nil.

Create a hash

Like arrays, there are different ways to create hashes. You can create an empty hash by the new class method:

months = Hash.new

You can also use new to create a hash with a default value, and a hash without a default value is nil:

months = Hash.new( "month" )

或

months = Hash.new "month"

When you access any key in a hash with a default value, if the key or value does not exist, the access hash returns the default value:

#!/usr/bin/ruby

months = Hash.new( "month" )

puts "#{months[0]}"
puts "#{months[72]}"
Try it out . . .


This results in the following:

month
month
#!/usr/bin/ruby

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

puts "#{H['a']}"
puts "#{H['b']}"
Try it out . . .


This results in the following:

100
200

You can use any Ruby object as a key or value, or even an array, so the following example is a valid one:

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

Hash built-in methods

We need an instance of the Hash object to call the Hash method. Here's how to create an instance of a Hash object:

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

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

Hash.new { |hash, key| block }

This returns a new hash that is populated with a given object. N ow, with the created object, we can call any available instance method. For example:

#!/usr/bin/ruby

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

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

keys = months.keys

puts "#{keys}"
Try it out . . .


This results in the following:

2, 1

Here's the common hash method (assuming hash is a Hash object):

序号 方法 & 描述
1 hash == other_hash
检查两个哈希是否具有相同的键值对个数,键值对是否相互匹配,来判断两个哈希是否相等。
2 hash.[key]
使用键,从哈希引用值。如果未找到键,则返回默认值。
3 hash.[key]=value
value 给定的值与 key 给定的键进行关联。
4 hash.clear
从哈希中移除所有的键值对。
5 hash.default(key = nil)
返回 hash 的默认值,如果未通过 default= 进行设置,则返回 nil。(如果键在 hash 中不存在,则 [] 返回一个默认值。)
6 hash.default = obj
hash 设置默认值。
7 hash.default_proc
如果 hash 通过块来创建,则返回块。
8 hash.delete(key) [or]
array.delete(key) { |key| block }

通过 key hash 中删除键值对。如果使用了块 且未找到匹配的键值对,则返回块的结果。把它与 delete_if 进行比较。
9 hash.delete_if { |key,value| block }
为 block 为 true 的每个块,从 hash 中删除键值对。
10 hash.each { |key,value| block }
遍历 hash ,为每个 key 调用一次 block,传递 key-value 作为一个二元素数组。
11 hash.each_key { |key| block }
遍历 hash ,为每个 key 调用一次 block,传递 key 作为参数。
12 hash.each_key { |key_value_array| block }
遍历 hash ,为每个 key 调用一次 block,传递 key value 作为参数。
13 hash.each_key { |value| block }
遍历 hash ,为每个 key 调用一次 block,传递 value 作为参数。
14 hash.empty?
检查 hash 是否为空(不包含键值对),返回 true false
15 hash.fetch(key [, default] ) [or]
hash.fetch(key) { | key | block }

通过给定的 key hash 返回值。如果未找到 key ,且未提供其他参数,则抛出 IndexError 异常;如果给出了 default ,则返回 default ;如果指定了可选的 block,则返回 block 的结果。
16 hash.has_key?(key) [or] hash.include?(key) [or]
hash.key?(key) [or] hash.member?(key)

检查给定的 key 是否存在于哈希中,返回 true false
17 hash.has_value?(value)
检查哈希是否包含给定的 value
18 hash.index(value)
为给定的 value 返回哈希中的 key ,如果未找到匹配值则返回 nil
19 hash.indexes(keys)
返回一个新的数组,由给定的键的值组成。找不到的键将插入默认值。该方法已被废弃,请使用 select。
20 hash.indices(keys)
返回一个新的数组,由给定的键的值组成。找不到的键将插入默认值。该方法已被废弃,请使用 select。
21 hash.inspect
返回哈希的打印字符串版本。
22 hash.invert
创建一个新的 hash ,倒置 hash 中的 keys values 。也就是说,在新的哈希中, hash 中的键将变成值,值将变成键。
23 hash.keys
创建一个新的数组,带有 hash 中的键。/td>
24 hash.length
以整数形式返回 hash 的大小或长度。
25 hash.merge(other_hash) [or]
hash.merge(other_hash) { |key, oldval, newval| block }

返回一个新的哈希,包含 hash other_hash 的内容,重写 hash 中与 other_hash 带有重复键的键值对。
26 hash.merge!(other_hash) [or]
hash.merge!(other_hash) { |key, oldval, newval| block }

与 merge 相同,但实际上 hash 发生了变化。
27 hash.rehash
基于每个 key 的当前值重新建立 hash 。如果插入后值发生了改变,该方法会重新索引 hash
28 hash.reject { |key, value| block }
block true 的每个键值对创建一个新的 hash
29 hash.reject! { |key, value| block }
reject 相同,但实际上 hash 发生了变化。
30 hash.replace(other_hash)
hash 的内容替换为 other_hash 的内容。
31 hash.select { |key, value| block }
返回一个新的数组,由 block 返回 true hash 中的键值对组成。
32 hash.shift
hash 中移除一个键值对,并把该键值对作为二元素数组返回。
33 hash.size
以整数形式返回 hash size 或 length。
34 hash.sort
hash 转换为一个包含键值对数组的二维数组,然后进行排序。
35 hash.store(key, value)
存储 hash 中的一个键值对。
36 hash.to_a
从 hash 中创建一个二维数组。每个键值对转换为一个数组,所有这些数组都存储在一个数组中。
37 hash.to_hash
返回 hash (self)。
38 hash.to_s
hash 转换为一个数组,然后把该数组转换为一个字符串。
39 hash.update(other_hash) [or]
hash.update(other_hash) {|key, oldval, newval| block}

返回一个新的哈希,包含 hash other_hash 的内容,重写 hash 中与 other_hash 带有重复键的键值对。
40 hash.value?(value)
检查 hash 是否包含给定的 value
41 hash.values
返回一个新的数组,包含 hash 的所有值。
42 hash.values_at(obj, ...)
返回一个新的数组,包含 hash 中与给定的键相关的值。