Ruby hash
Hash is similar "key" => "value"
such a collection of key-value pairs. A hash is similar to an array, except that its index is not limited to using numbers.
Hash
the index (or “key”) can be almost any object.
Hash
although similar to arrays, there is one important difference: the elements of Hash
do not have a specific order. Use arrays if order is important.
Create a hash
Like arrays, there are different ways to create hashes. You can use the new
class method to create an empty hash:
months=Hash.new
You can also use the new
create a hash with default values, and hashes without default values are 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:
Example
#!/usr/bin/rubymonths=Hash.new("month")puts"#{months[0]}"puts"#{months[72]}"
The output of the above instance is as follows:
month
month
Example
#!/usr/bin/rubyH=Hash["a"=>100,"b"=>200]puts"#{H['a']}"puts"#{H['b']}"
The output of the above instance is as follows:
100
200
You can use any Ruby object as a key or value, or even an array, as shown inthe following example:
[1,"jan"]=>"January"
Hash built-in method
If you need to call Hash
method, you need to instantiate a Hash
object. The following is the creation of Hash
method of the object instance:
Hash[[key=>|,value]*]orHash.new[or]Hash.new(obj)[or]Hash.new{
\|hash,key\|block}
This returns a new hash populated with the given object. Now, using the created object, we can call any available method. For example:
Example
#!/usr/bin/ruby$,=","months=Hash.new("month")months=
{"1"=>"January","2"=>"February"}keys=months.keysputs"#{keys}"
The output of the above instance is as follows:
["1", "2"]
The following is the common hash method (assuming hash
is a. Hash
object):
Serial number |
Method and description |
---|---|
1 |
Hash = = other_hash checks whether two hashes have the same number of key-value pairs and whether they match each other to determine whether the two hashes are equal. |
2 |
Hash [key] Use the key to reference the value from the hash. If the key is not found, the default value is returned. |
3 |
Hash [key] = value associates the value given by value with the key given bykey. |
4 |
Hash.clear removes all key-value pairs from the hash. |
5 |
Hash.default (key = nil) returns the default value of hash, or nil if it is not set through default=. If the key does not exist in hash, [] returns a default value. ) |
6 |
Hash.default = obj sets the default value for hash. |
7 |
Hash.default_proc returns a block if the hash is created from a block. |
8 |
hash.delete(key) [or] array.delete(key) { |
9 |
hash.delete_if { |
10 |
hash.each { |
11 |
hash.each_key { |
12 |
hash.each_key { |
13 |
hash.each_value { |
14 |
Hash.empty? Check whether hash is empty (without key-value pairs) and returntrue or false. |
15 |
hash.fetch(key [, default] ) [or] hash.fetch(key) { |
16 |
Hash.has_key? (key) [or] Hash.include? (key) [or] Hash.key? (key) [or] Hash.member? (key) checks whether the given key exists in the hash and returns true or false. |
17 |
Hash.has_value? (value) checks whether the hash contains the given value. |
18 |
Hash.index (value) returns the key in the hash for the given value, or nil if no match is found. |
19 |
Hash.indexes (keys) returns a new array consisting of the values of the given key. Keys that cannot be found are inserted into the default value. This method is obsolete, please use select. |
20 |
Hash.indices (keys) returns a new array consisting of the values of the given key. Keys that cannot be found are inserted into the default value. This method is obsolete, please use select. |
21 |
Hash.inspect returns the printed string version of the hash. |
22 |
Hash.invert creates a new hash, inverting keys and values in hash. That is, in the new hash, the key in hash becomes the value, and the value becomes the key. |
23 |
Hash.keys creates a new array with the keys in hash. |
24 |
Hash.length returns the size or length of the hash as an integer. |
25 |
hash.merge(other_hash) [or] hash.merge(other_hash) { |
26 |
hash.merge!(other_hash) [or] hash.merge!(other_hash) { |
27 |
Hash.rehash re-establishes the hash based on the current value of each key. If the value changes after insertion, the method re-indexes the hash. |
28 |
hash.reject { |
29 |
hash.reject! { |
30 |
Hash.replace (other_hash) replaces the content of hash with the content of other_hash. |
31 |
Hash. select { |
32 |
Hash.shift removes a key-value pair from hash and returns the key-value pairas a binary array. |
33 |
Hash.size returns the size or length of hash as an integer. |
34 |
Hash.sort converts the hash to a two-dimensional array containing an array of key-value pairs, and then sorts it. |
35 |
Hash.store (key, value) stores a key-value pair in hash. |
36 |
Hash.to_a creates a two-dimensional array from hash. Each key-value pair is converted to an array, all of which are stored in an array. |
37 |
Hash.to_hash returns hash (self). |
38 |
Hash.to_s converts hash to an array and then converts the array to a string. |
39 |
Hash. update (other_hash) [or] hash. update (other_hash) { |
40 |
Hash.value? (value) checks whether the hash contains the given value. |
41 |
Hash.values returns a new array containing all the values of hash. |
42 |
Hash.values_at (obj,…) returns a new array containing the values in hash related to the given key. |