Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Hashes (Hash)

Ruby Hashes (Hash)

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

The index of a Hash (or "key") can be almost any object.

Although a Hash is similar to an array, there is one important difference: the elements of a Hash have no particular order. If order matters, you should use an array.

Creating a hash

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

months

=

Hash

.

new

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

months

=

Hash

.

new

(

"

month

"

)

or

months

=

Hash

.

new

"

month

"

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

Example

!/usr/bin/ruby

months

=

Hash

.

new

(

"

month

"

)

puts

"

#{months[0]}

"

puts

"

#{months[72]}

"

Try it »

The above example produces the following output:


month
month

Example

!/usr/bin/ruby

H

=

Hash

[

"

a

"

=>

100

,

"

b

"

=>

200

]

puts

"

#{H['a']}

"

puts

"

#{H['b']}

"

Try it »

The above example produces the following output:


100
200

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

[

1

,

"

jan

"

]

=>

"

January

"

Hash Built-in Methods

If you need to call a Hash method, you first need to instantiate a Hash object. Here is how to create a Hash object instance:

Hash

[

[

key

=>|,

value

]

]

or

Hash

.

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

.

keys

puts

"

#{keys}

"

Try it »

The above example produces the following output:


["1", "2"]

The following are the public hash methods (assuming hash is a Hash object):

No. Method & Description
1 hash == other_hash
Checks whether two hashes have the same number of key-value pairs and whether the pairs match each other, to determine whether the two hashes are equal.
2 hash[key]
References a value from the hash using a key. If the key is not found, returns the default value.
3 hash[key]=value
Puts the value Associates the given value with key the given key.
4 hash.clear
Removes all key-value pairs from the hash.
5 hash.default(key = nil)
Return hash The default value of; if not set via default=, returns nil. (If the key does not exist in hash then [] returns a default value.)
6 hash.default = obj
is hash Sets the default value.
7 hash.default_proc
If both the hash If created with a block, returns the block.
8 **hash.delete(key) [or]
array.delete(key) { |key| block }**
Pass key From the hash Deletes key-value pairs from. If a block is used and no matching pair is found, returns the result of the block. Compare it with delete_if for comparison.
9 hash.delete_if { |key,value| block }
block is true for each block of, from hash deletes key-value pairs.
10 hash.each { |key,value| block }
Iteration hash, for each key calls the block once, passing key-value as a two-element array.
11 hash.each_key { |key| block }
Iteration hash, for each key calls the block once, passing key as its argument.
12 hash.each_key { |key_value_array| block }
Iteration hash, for each key calls the block once, passing key and value as its argument.
13 hash.each_value { |value| block }
Iteration hash, for each key calls the block once, passing value as its argument.
14 hash.empty?
Checks whether the hash is empty (contains no key-value pairs), returning true or false。
15 **hash.fetch(key [, default] ) [or]
hash.fetch(key) { | key | block }**
by the given key From the hash returns a value. If not found key, and no other argument is provided, raises IndexError exception; if given default, return default; if an optional block is specified, returns the result of the block.
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, returning true or false。 | | 17 | hash.has_value?(value)
Checks whether the hash contains the given value。 | | 18 | hash.index(value)
For the given value Returns the in the hash key, and returns if no matching value is found nil。 | | 19 | hash.indexes(keys)
Returns a new array consisting of the values for the given keys. Keys that are not found will be inserted with the default value. This method is deprecated; please use select. | | 20 | hash.indices(keys)
Returns a new array consisting of the values for the given keys. Keys that are not found will be inserted with the default value. This method is deprecated; please use select. | | 21 | hash.inspect
Returns a printable string version of the hash. | | 22 | hash.invert
Creates a new hash, with inverted hash The keys and values. That is, in the new hash,hash keys in become values, and values become keys. | | 23 | hash.keys
Creates a new array with hash the keys in. | | 24 | hash.length
Returns, as an integer, the hash The size or length of. | | 25 | hash.merge(other_hash) [or]
hash.merge(other_hash) { |key, oldval, newval| block }

Returns a new hash containing hash and other_hash the contents of, overriding key-value pairs in hash with those in other_hash that have duplicate keys. | | 26 | hash.merge!(other_hash) [or]
hash.merge!(other_hash) { |key, oldval, newval| block }

Same as merge, but hash is actually changed. | | 27 | hash.rehash
Based on the current value of each key rebuilds hash. If the value changes after insertion, this method re-indexes hash。 | | 28 | hash.reject { |key, value| block }
Similar to delete_if, but operates on a copy of the hash. Equivalent to hsh.dup.delete_if. | | 29 | hash.reject! { |key, value| block }
Equivalent to delete_if, but returns nil if no changes were made. | | 30 | hash.replace(other_hash)
Puts the hash Replaces the contents of with other_hash the contents of. | | 31 | hash.select { |key, value| block }
Returns a new array consisting of block Return true 's hash the key-value pairs in. | | 32 | hash.shift
From the hash Removes a key-value pair from and returns that pair as a two-element array. | | 33 | hash.size
Returns, as an integer, the hash 's size or length. | | 34 | hash.sort
Puts the hash Converts to a two-dimensional array containing arrays of key-value pairs, then sorts. | | 35 | hash.store(key, value)
Storage hash a key-value pair from. | | 36 | hash.to_a
Creates a two-dimensional array from the hash. Each key-value pair is converted to an array, and all these arrays are stored in one array. | | 37 | hash.to_hash
Return hash(self)。 | | 38 | hash.to_s
Puts the hash Converts to an array, then converts that array to a string. | | 39 | hash.update(other_hash) [or]
hash.update(other_hash) {|key, oldval, newval| block}

Returns a new hash containing hash and other_hash the contents of, overriding hash in with other_hash key-value pairs that have duplicate keys. | | 40 | hash.value?(value)
Checks hash Whether it contains the given value。 | | 41 | hash.values
Returns a new array containing hash all values of. | | 42 | hash.values_at(obj, ...)
Returns a new array containing hash The value in associated with the given key. |