Ruby Hash

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 the use of numbers.

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

Hash is similar to arrays, but it has one important difference: Hash elements have no specific order. If the order is important, use an array.

Create a hash

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

months = Hash.new

You can also use new to create a hash with default values. The hash without the default value is nil:

months = Hash.new( "month" ) or months = Hash< Span class="hl-code">.new "month"

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


#!/usr/bin/ruby months = Hash.new( "month" ) puts "#{months[0]}" puts "#{months[72]}"

The above example runs the output as:



#!/usr/bin/ruby H = Hash["a" => 100, "b" => 200] puts "#{H['a']}" puts "#{H['b']}"

The above example runs the output as:


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 instantiate a Hash object first. 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 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 above precedent runs the yield as:

["1",  "2"]

The following is an open hash strategy (accepting hash is a Hash object):

serial numbermethod & description
1hash == other_hash
Check if the two hashes have a similar number of key-esteem sets, and if the key-esteem sets coordinate one another, To decide whether the two hashes are equivalent.
Use the way to reference the incentive from the hash. On the off chance that the key isn't discovered, the default esteem is returned.
put the esteem given by value with key The related key is related.
Remove all key-esteem sets from the hash.
5hash.default(key = nil)
Returns the default estimation of hash on the off chance that it doesn't pass default= Set it to return nil. (On the off chance that the key does not exist in hash, [] restores a default value.)
6hash.default = obj
Set the default an incentive for hash.
If hash is made by a square, the square is returned.
8hash.delete(key) [or]
Array.delete(key) { |key| square }

Delete key-esteem sets from hash by means of key. On the off chance that a square is utilized and no coordinating key-esteem sets are discovered, the aftereffect of the square is returned. Contrast it with delete_if.
9hash.delete_if { |key,value| square }
square is true for each square, Remove key-esteem sets from hash.
10hash.each { |key,value| square }
navigate hash for each key Calls a square once and passes the key-esteem as a two-component cluster.
11hash.each_key { |key| square }
navigate hash for each key Call the square once and pass key as a parameter.
12hash.each_key { |key_value_array| square }
navigate hash for each key Call the square once, passing key and value as contentions.
13hash.each_value { |value| square }
navigate hash for each key Call the square once and pass value as a parameter.
Check if the hash is unfilled (does not contain key-esteem matches) and return true Or false.
15hash.fetch(key [, default] ) [or]
Hash.fetch(key) { | key | square }

Returns an incentive from hash with the given key. On the off chance that key isn't found and no different parameters are provided, a IndexError exemption is tossed; if default is given, default is returned ; If a discretionary square is indicated, the consequence of the square is returned.
16hash.has_key?(key) [or] hash.include?(key) [or]
Hash.key?(key) [or] hash.member?(key)

Checks if the given key exists in the hash and returns true or false.
Check if the hash contains the given value.
Returns the in the hash for the given value Key, or nil if no match is found.
Returns another cluster comprising of the qualities ​​of the given keys. The key not found will be embedded into the default esteem. This strategy has been expostulated, it would be ideal if you utilize select.
Returns another cluster comprising of the qualities ​​of the given keys. The key not found will be embedded into the default esteem. This technique has been belittled, kindly utilize select.
Returns the printed string rendition of the hash.
Create another hash, transform hash keys and values. That is, in the new hash, the key in hash will turn into the esteem and the esteem will turn into the key.
Create another cluster with keys from hash. /td>
Returns the size or length of hash as a whole number.
25hash.merge(other_hash) [or]
Hash.merge(other_hash) { |key, oldval, newval| square }

Returns another hash containing the substance of hash and other_hash , modifying the key-esteem matches in the hash with other_hash with copy keys.
26hash.merge!(other_hash) [or]
Hash.merge!(other_hash) { |key, oldval, newval| square }

Same as consolidation, however the hash really changes.
Re-build up hash dependent on the present estimation of each key >. In the event that the esteem changes subsequent to embeddings, the strategy will re-record hash.
28hash.reject { |key, value| square }
Like delete_if, yet on a hash of a duplicate. Comparable to hsh.dup.delete_if.
29hash.reject! { |key, value| square }
is equivalent to delete_if, however returns nil in the event that it has not been changed.
Replace the substance of hash with other_hash > content.
31hash.select { |key, value| square }
Returns another cluster by block Returns the key-esteem combines in the hash of true.
Remove a key-esteem pair from hash and put the key-esteem pair Returned as a two-component exhibit.
Return size or length of hash as a whole number .
Convert hash to a two-dimensional exhibit containing a variety of key-esteem sets, at that point put all together.
35hash.store(key, value)
Store a key-esteem pair in hash.
Create a two-dimensional cluster from hash. Each key-esteem pair is changed over to a cluster, and these exhibits are put away in a cluster.
Return hash(self).
Convert hash to an exhibit and convert the cluster to a string .
39hash.update(other_hash) [or]
Hash.update(other_hash) {|key, oldval, newval| block}

Returns another hash containing the substance of hash and other_hash , superseding hash with other_hash key-esteem sets with copy keys.
Check if hash contains the given value.
Returns another cluster containing every one of the qualities ​​of hash.
42hash.values_at(obj, ...)
Returns another exhibit containing hash The esteem related with the given key.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.