Latest web development tutorials

Ruby hash (Hash)

Hash (Hash) is similar to the "key" => "value" of such a collection of key-value pairs. Similar to a hash array, but it is not limited to the use of index numbers.

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

Hash array Although similar, but there is a very important distinction: Hash elements are in no particular order. If the order is important, then you should use the array.

Creating a hash

Like an array, a variety of different ways to create a hash. You can create an empty hash throughnewclass method:

months = Hash.new

You can also use thenewhash is created with default values, without default hash value isnil:

months = Hash.new( "month" )

或

months = Hash.new "month"

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

#!/usr/bin/ruby

months = Hash.new( "month" )

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

Run the above example output is:

month
month
#!/usr/bin/ruby

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

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

Run the above example output is:

100
200

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

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

Hash built-in method

If you need to call the Hash method, you need to instantiate a Hash object. Here is a way 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 given object filled with new hash. Now, using objects created, we can call any of the available methods. E.g:

#!/usr/bin/ruby

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

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

keys = months.keys

puts "#{keys}"

Run the above example output is:

["1", "2"]

The following are common hash method (assuming ahashHash object):

No. Method & description
1 hash == other_hash
Check whether the two have the same hash key value for the number of key-value pairs are matched with each other to determine whether the two hashes are equal.
2 hash. [key]
Use the key reference values ​​from the hash. If the key is not found, then return to the default values.
3 hash. [key] = value
Thevaluegiven the value of thekeyassociated with the given key.
4 hash.clear
Remove all keys from the hash.
5 hash.default (key = nil)
Returns the default value ofhash,if not set by default =, returns nil. (If the key does not exist in thehash,then [] returns a default value.)
6 hash.default = obj
Set the default values for thehash.
7 hash.default_proc
If thehashcreated by the block, the block returned.
8 hash.delete (key) [or]
array.delete (key) {| key | block}
Remove from thehashkey bykeypair. If the block and did not find a matching key-value pairs, the results of the block is returned. Compare it withdelete_if.
9 hash.delete_if {| key, value | block }
It istruefor each block of the block, delete the key fromthe hash.
10 hash.each {| key, value | block }
Traverse thehash,called once for eachkeyblock, pass key-value as a two-element array.
11 hash.each_key {| key | block}
Traverse thehash,called once for eachkeyblock, passkeyas a parameter.
12 hash.each_key {| key_value_array | block}
Traverse thehash,called once for eachkeyblock, passkeyandvalueas arguments.
13 hash.each_value {| value | block}
Traverse thehash,called once for eachkeyblock, passed as a parametervalue.
14 hash.empty?
Check whether the hash is empty (does not contain the key-value pairs), and returnstrueorfalse.
15 hash.fetch (key [, default]) [or]
hash.fetch (key) {| key | block}
The return value from thehashthrough a givenkey.If you do not find thekey,and did not provide additional parameters, throwIndexErrorexception; if givendefault,returnsdefault;if the optional block, block result is returned.
16 hash.has_key? (key) [or] hash.include? (key) [or]
hash.key? (key) [or] hash.member? (key)
Check if the givenkeyexists in the hash, returnstrueorfalse.
17 hash.has_value? (value)
Check whether the hash contains the givenvalue.
18 hash.index (value)
Returns for a givenvalueof the hashkey,if no match is found returnsnil.
19 hash.indexes (keys)
It returns a new array by the value of a given key components. Can not find the key to insert the default value. This method has been deprecated, please use the select.
20 hash.indices (keys)
It returns a new array by the value of a given key components. Can not find the key to insert the default value. This method has been deprecated, please use the select.
twenty one hash.inspect
Back Print version string hash.
twenty two hash.invert
Create a newhash,hash inversion ofkeysandvalues.In other words, the newhash,hash value becomes the key, the value will become key.
twenty three hash.keys
Create a new array with thehashkey. / Td>
twenty four hash.length
It returns an integerhashsize or length.
25 hash.merge (other_hash) [or]
hash.merge (other_hash) {| key, oldval, newval | block}
Returns a new hash,hashcontains content andother_hashrewrites key hash withother_hashwith a duplicate key pair.
26 hash.merge! (other_hash) [or]
hash.merge (other_hash)! {| key , oldval, newval | block}
And merge the same, but actually hash has changed.
27 hash.rehash
Re-establish thehashbased on the current value of eachkey.If you insert the value has changed, the process will be re-indexedhash.
28 hash.reject {| key, value | block }
As istrueof each keyblockto create a newhash.
29 hash.reject! {| key, value | block}
Andrejectthe same, but actually hash has changed.
30 hash.replace (other_hash)
Replace the contents of thehashof the contentother_hash.
31 hash.select {| key, value | block }
Returns a new array, theblockreturnstruehash key values in the pairs.
32 hash.shift
Remove from thehashin a key pair and sends the key value returned as a two-element array.
33 hash.size
It returns an integerhashof thesizeor length.
34 hash.sort
Thehashis converted to a two-dimensional array containing key-value array and then sorted.
35 hash.store (key, value)
Hashstored in a key-value pair.
36 hash.to_a
Create a two-dimensional array from the hash. Each key-value pair into an array, all these arrays are stored in an array.
37 hash.to_hash
Returnshash (self).
38 hash.to_s
Thehashis converted to an array, then the array is converted to a string.
39 hash.update (other_hash) [or]
hash.update (other_hash) {| key, oldval, newval | block}
Returns a new hash,hashcontains content andother_hashrewrites keyhashwithother_hashwith a duplicate key pair.
40 hash.value? (value)
Check whether thehashcontains the givenvalue.
41 hash.values
It returns a new array containing all the values of thehash.
42 hash.values_at (obj, ...)
It returns a new array containing thehashvalue associated with a given key.