Ruby Hash


Hash is a collection of key-value pairs like "key" => "value". A hash is similar to an array, except that its indexing is not limited to using numbers.

Hash's index (or "key") can be almost any object.

Although Hash is similar to an array, it has an important difference: the elements of Hash have no specific order. If order is important then use an array.

Creating a Hash

As with arrays, there are various ways to create a hash. You can create an empty hash via the new class method:

months = Hash.new

You can also create a hash with and without the default value using new The hash is nil:

months = Hash.new( "month" )

或

months = Hash.new "month"

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

#!/usr/bin/ruby

months = Hash.new( "month" )

puts "#{months[0]}"
puts "#{months[72]}"


The output result of the above instance is:

month
month
#!/usr/bin/ruby

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

puts "#{H['a']}"
puts "#{H['b']}"


The output result of the above instance is:

100
200

You can use any Ruby object as a key or value, or even an array, as shown in the following example:

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

Hash built-in method

If you need to call the Hash method, you need to first Instantiate a Hash object. Here's how to create a Hash object instance:

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

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

Hash.new { |hash, key| block }

This will return a new hash populated with the given object. Now, using the created object, we can call any of the available methods. For example:

#!/usr/bin/ruby

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

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

keys = months.keys

puts "#{keys}"


The output result of the above example is:

["1", "2"]

The following is a public hash method (assuming hash is a Hash object):

23242526272829hash.reject! { |key, value| block }30hash.replace(other_hash)
Serial numberMethod & Description
1hash == other_hash
Check whether two hashes have the same number of key-value pairs and whether the key-value pairs match each other to determine whether the two hashes are equal.
2hash.[key]
Use a key to reference a value from a hash. If the key is not found, the default value is returned.
3hash.[key]=value
Compare the value given by value with key Association with the given key.
4hash.clear
Remove all key-value pairs from the hash.
5hash.default(key = nil)
Returns the default value of hash, if not If set via default=, nil will be returned. (If the key does not exist in hash, [] returns a default value.)
6hash.default = obj
Sets the default value for hash.
7hash.default_proc
If hash is created via a block, the block is returned.
8hash.delete(key) [or]
array.delete(key) { |key| block }

Delete key-value pairs from hash via key. If a block is used and no matching key-value pair is found, the result of the block is returned. Compare this to delete_if.
9hash.delete_if { |key,value| block }
is block for true Each block, remove key-value pairs from hash.
10hash.each { |key,value| block }
Traverse hash, for each Each key calls block once, passing key-value as a two-element array.
11hash.each_key { |key| block }
Traverse hash, for each key Call the block once, passing key as the parameter.
12hash.each_key { |key_value_array| block }
Traverse hash, for each key Call the block once, passing key and value as parameters.
13hash.each_value { |value| block }
Traverse hash, for each key Call the block once, passing value as the parameter.
14hash.empty?
Check whether the hash is empty (does not contain key-value pairs) and returns true or false.
15hash.fetch(key [, default] ) [or]
hash.fetch(key) { | key | block }

Returns the value from hash given the given key. If key is not found and no other parameters are supplied, an IndexError exception is thrown; if default is given, default is returned ;If the optional block is specified, the result of the block is returned.
16hash.has_key?(key) [or] hash.include?(key) [or]
hash.key?( key) [or] hash.member?(key)

Checks whether the given key exists in the hash, returns true or false.
17hash.has_value?(value)
Checks if the hash contains the given value .
18hash.index(value)
Returns the hash for the given value key, or nil if no matching value is found.
19hash.indexes(keys)
Returns a new array consisting of the values ​​of the given keys. Keys not found will have default values ​​inserted. This method is deprecated, please use select.
20hash.indices(keys)
Returns a new array consisting of the values ​​of the given keys. Keys not found will have default values ​​inserted. This method is deprecated, please use select.
21hash.inspect
Returns the printed string version of the hash.
22hash.invert
Create a new hash, invert hash##keys and values in #. That is, in the new hash, the keys in hash will become values, and the values ​​will become keys.
hash.keysCreates a new array with the keys in
hash. /td>
hash.lengthReturns the size or length of
hash in integer form.
hash.merge(other_hash) [or]hash.merge(other_hash) { |key, oldval, newval| block }
Returns a new hash containing the contents of
hash and other_hash, rewriting the keys in the hash with duplicate keys from other_hash value pair.
hash.merge!(other_hash) [or]hash.merge!(other_hash) { |key, oldval, newval| block }
is the same as merge, but in fact the hash has changed.
hash.rehashRebuild the
hash based on the current value of each key . This method will re-index hash if the value has changed since insertion.
hash.reject { |key, value| block } is
block is ## Create a new hash for each key-value pair of #true.
is the same as reject
, But in fact the hash has changed.
Replace the contents of hash with the contents of other_hash.
31hash.select { |key, value| block }
Returns a new array, consisting of block Returns the key-value pairs in hash of true.
32hash.shift
Remove a key-value pair from hash and shift the Key-value pairs are returned as a two-element array.
33hash.size
Returns the size of hash in integer form or length.
34hash.sort
Convert hash into a two-dimensional array containing key-value pairs array and then sort it.
35hash.store(key, value)
Storage a key-value pair in hash .
36hash.to_a
Create a two-dimensional array from hash. Each key-value pair is converted to an array and all these arrays are stored in an array.
37hash.to_hash
returns hash(self).
38hash.to_s
Convert hash to an array, and then convert the array to a string.
39hash.update(other_hash) [or]
hash.update(other_hash) {|key, oldval, newval| block}

Returns a new hash containing the contents of hash and other_hash, rewriting hash with other_hash Key-value pairs with duplicate keys.
40hash.value?(value)
Check if hash contains the given value.
41hash.values
Returns a new array containing all the values ​​of hash.
42hash.values_at(obj, ...)
Returns a new array containing hash The value associated with the given key.