"value" such a collection of key-value pairs. A hash is similar to an array, except that its ...">

Ruby hash


Release date:2023-10-28 Update date:2023-10-28 Editor:admin View counts:210

Label:

Ruby hash

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

Hash the index (or “key”) can be almost any object.

Hash although similar to arrays, there is one important difference: the elements of Hash do not have a specific order. Use arrays if order is important.

Create a hash

Like arrays, there are different ways to create hashes. You can use the new class method to create an empty hash:

months=Hash.new

You can also use the new create a hash with default values, and hashes without default values are nil :

months=Hash.new("month")或months=Hash.new"month"

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

Example

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

The output of the above instance is as follows:

month
month

Example

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

The output of the above instance is as follows:

100
200

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

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

Hash built-in method

If you need to call Hash method, you need to instantiate a Hash object. The following is the creation of Hash method of the object instance:

Hash[[key=>|,value]*]orHash.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.keysputs"#{keys}"

The output of the above instance is as follows:

["1", "2"]

The following is the common hash method (assuming hash is a. Hash object):

Serial number

Method and description

1

Hash = = other_hash checks whether two hashes have the same number of key-value pairs and whether they match each other to determine whether the two hashes are equal.

2

Hash [key] Use the key to reference the value from the hash. If the key is not found, the default value is returned.

3

Hash [key] = value associates the value given by value with the key given bykey.

4

Hash.clear removes all key-value pairs from the hash.

5

Hash.default (key = nil) returns the default value of hash, or nil if it is not set through default=. If the key does not exist in hash, [] returns a default value. )

6

Hash.default = obj sets the default value for hash.

7

Hash.default_proc returns a block if the hash is created from a block.

8

hash.delete(key) [or] array.delete(key) { |key| block } Delete key-value pairs from the hash using key. If a block is used and no matching key value pair is found, the result of the block is returned. Compare it with delete_ Compare if.

9

hash.delete_if { |key,value| block } remove key value pairs from the hash for each block that is true.

10

hash.each { |key,value| block } traverse hash, call block once for each key, passing key value as a binary array.

11

hash.each_key { |key| block } traverses the hash, calling a block once for each key, passing the key as a parameter.

12

hash.each_key { |key_value_array| block } traverse hash, call block once for each key, passing value and key as parameters.

13

hash.each_value { |value| block } traverse hash, call block once for each key, passing value as a parameter.

14

Hash.empty? Check whether hash is empty (without key-value pairs) and returntrue or false.

15

hash.fetch(key [, default] ) [or] hash.fetch(key) { | key | block } Returns a value from the hash using the given key. If the key is not found and no other parameters are provided, an IndexError exception will be thrown; If default is given, return default; If an optional block is specified, the result of the block is returned.

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 and returns true or false.

17

Hash.has_value? (value) checks whether the hash contains the given value.

18

Hash.index (value) returns the key in the hash for the given value, or nil if no match is found.

19

Hash.indexes (keys) returns a new array consisting of the values of the given key. Keys that cannot be found are inserted into the default value. This method is obsolete, please use select.

20

Hash.indices (keys) returns a new array consisting of the values of the given key. Keys that cannot be found are inserted into the default value. This method is obsolete, please use select.

21

Hash.inspect returns the printed string version of the hash.

22

Hash.invert creates a new hash, inverting keys and values in hash. That is, in the new hash, the key in hash becomes the value, and the value becomes the key.

23

Hash.keys creates a new array with the keys in hash.

24

Hash.length returns the size or length of the hash as an integer.

25

hash.merge(other_hash) [or] hash.merge(other_hash) { |key, oldval, newval| block } returns a new hash, including hash and other_ the

26

hash.merge!(other_hash) [or] hash.merge!(other_hash) { |key, oldval, newval| block } same as merge, but the hash has actually changed.

27

Hash.rehash re-establishes the hash based on the current value of each key. If the value changes after insertion, the method re-indexes the hash.

28

hash.reject { |key, value| block }Similar to delete_ If, but on a copy of the hash. Equivalent to hsh. dup. delete_ If.

29

hash.reject! { |key, value| block } is equivalent to delete_ If, but if there are no modifications, return nil.

30

Hash.replace (other_hash) replaces the content of hash with the content of other_hash.

31

Hash. select { |key, value| block } returns a new array consisting of key value pairs in the hash where block returns true.

32

Hash.shift removes a key-value pair from hash and returns the key-value pairas a binary array.

33

Hash.size returns the size or length of hash as an integer.

34

Hash.sort converts the hash to a two-dimensional array containing an array of key-value pairs, and then sorts it.

35

Hash.store (key, value) stores a key-value pair in hash.

36

Hash.to_a creates a two-dimensional array from hash. Each key-value pair is converted to an array, all of which are stored in an array.

37

Hash.to_hash returns hash (self).

38

Hash.to_s converts hash to an array and then converts the array to a string.

39

Hash. update (other_hash) [or] hash. update (other_hash) { |key, oldval, newval| block} returns a new hash containing attributes of hash and other_hash, overwriting key value pairs in the hash that have duplicate keys with other_hash.

40

Hash.value? (value) checks whether the hash contains the given value.

41

Hash.values returns a new array containing all the values of hash.

42

Hash.values_at (obj,…) returns a new array containing the values in hash related to the given key.

Powered by TorCMS (https://github.com/bukun/TorCMS).