home PYTHONJAVA
 

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:

Instance

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

The above example runs the output as:

month
Month

Instance

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

The above example runs the output as:

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 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:

Instance

#!/usr/bin/ruby $, = ", " months = Hash.new( "month" ) months = {"1" => "January", "2" => "February"} keys = months.keys puts "#{keys}"

The above example runs the output as:

["1",  "2"]

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

serial numbermethod & description
1hash == other_hash
Check if the two hashes have the same number of key-value pairs, and if the key-value pairs match each other, To determine if the two hashes are equal.
2hash.[key]
Use the key to reference the value from the hash. If the key is not found, the default value is returned.
3hash.[key]=value
put the value given by value with key< /i> The associated key is associated.
4hash.clear
Remove all key-value pairs from the hash.
5hash.default(key = nil)
Returns the default value of hash if it does not pass default= Set it to return nil. (If the key does not exist in hash, [] returns a default value.)
6hash.default = obj
Set the default value for hash.
7hash.default_proc
If hash is created by 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 pairs are found, the result of the block is returned. Compare it to delete_if.
9hash.delete_if { |key,value| block }
block is true for each block, Remove key-value pairs from hash.
10hash.each { |key,value| block }
traverse hash for each key Calls a block once and passes the key-value as a two-element array.
11hash.each_key { |key| block }
traverse hash for each key Call the block once and pass key as a parameter.
12hash.each_key { |key_value_array| block }
traverse hash for each key Call the block once, passing key and value as arguments.
13hash.each_value { |value| block }
traverse hash for each key Call the block once and pass value as a parameter.
14hash.empty?
Check if the hash is empty (does not contain key-value pairs) and return true Or false.
15hash.fetch(key [, default] ) [or]
Hash.fetch(key) { | key | block }

Returns a value from hash with 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 an 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 if the given key exists in the hash and returns true or false.
17hash.has_value?(value)
Check if the hash contains the given value.
18hash.index(value)
Returns the in the hash for the given value Key, or nil if no match is found.
19hash.indexes(keys)
Returns a new array consisting of the values ​​of the given keys. The key not found will be inserted into the default value. This method has been deprecated, please use select.
20hash.indices(keys)
Returns a new array consisting of the values ​​of the given keys. The key not found will be inserted into the default value. This method has been 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. That is, in the new hash, the key in hash will become the value and the value will become the key.
23hash.keys
Create a new array with keys from hash. /td>
24hash.length
Returns the size or length of hash as an integer.
25hash.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 key-value pairs in the hash with other_hash with duplicate keys.
26hash.merge!(other_hash) [or]
Hash.merge!(other_hash) { |key, oldval, newval| block }

Same as merge, but the hash actually changes.
27hash.rehash
Re-establish hash based on the current value of each key >. If the value changes after inserting, the method will re-index hash.
28hash.reject { |key, value| block }
Similar to delete_if, but on a hash of a copy. Equivalent to hsh.dup.delete_if.
29hash.reject! { |key, value| block }
is equal to delete_if, but returns nil if it has not been modified.
30hash.replace(other_hash)
Replace the contents of hash with other_hash > content.
31hash.select { |key, value| block }
Returns a new array by block Returns the key-value pairs in the hash of true.
32hash.shift
Remove a key-value pair from hash and put the key-value pair Returned as a two-element array.
33hash.size
Return size or length of hash as an integer .
34hash.sort
Convert hash to a two-dimensional array containing an array of key-value pairs, then put in order.
35hash.store(key, value)
Store 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 of these arrays are stored in an array.
37hash.to_hash
Return hash(self).
38hash.to_s
Convert hash to an array and 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 , overriding hash with other_hash key-value pairs with duplicate keys.
40hash.value?(value)
Check if hash contains the given value< /i>.
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.





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