| Start |
|---|
| Creating a hash A hash in Ruby is an object that implements a hash table (opens new window), mapping keys to values. Ruby supports a specific literal syntax for defining hashes using A hash can also be created using the standard Hashes can have values of any type, including complex types like arrays, objects and other hashes: Also keys can be of any type, including complex ones: Symbols (opens new window) are commonly used as hash keys, and Ruby 1.9 introduced a new syntax specifically to shorten this process. The following hashes are equivalent: The following hash (valid in all Ruby versions) is different, because all keys are strings: While both syntax versions can be mixed, the following is discouraged. With Ruby 2.2+, there is an alternative syntax for creating a hash with symbol keys (most useful if the symbol contains spaces): |
| Setting Default Values By default, attempting to lookup the value for a key which does not exist will return The default value of a hash can be passed to its constructor: A default can also be specified on an already constructed Hash: It is important to note that the default value is not copied each time a new key is accessed, which can lead to surprising results when the default value is a reference type: To circumvent this problem, the Hash constructor accepts a block which is executed each time a new key is accessed, and the returned value is used as the default: Note that above we had to use += instead of << because the default value is not automatically assigned to the hash; using << would have added to the array, but authors[:homer] would have remained undefined: In order to be able to assign default values on access, as well as to compute more sophisticated defaults, the default block is passed both the hash and the key: You can also use a default block to take an action and/or return a value dependent on the key (or some other data): You can even create more complex hashes: In order to set the default to a Proc on an already-existing hash, use |
| Accessing Values Individual values of a hash are read and written using the By default, accessing a key which has not been added to the hash returns Hashes can also contain keys in strings. If you try to access them normally it will just return a For situations where keys are expected or required to exist, hashes have a
Hashes also support a You can also get all values of a hash using the Note: This is only for Ruby 2.3+ |
| Automatically creating a Deep Hash Hash has a default value for keys that are requested but don't exist (nil): When creating a new Hash, one can specify the default: Hash.new also takes a block, which allows you to automatically create nested hashes, such as Perl's autovivification behavior or |
| Iterating Over a Hash A
|
| Conversion to and from Arrays Hashes can be freely converted to and from arrays. Converting a hash of key/value pairs into an array will produce an array containing nested arrays for pair: In the opposite direction a Hash can be created from an array of the same format: Similarly, Hashes can be initialized using Or from an array of arrays with two values each: Hashes can be converted back to an Array of alternating keys and values using The easy conversion to and from an array allows |
| Filtering hashes
When you will not need the key or value in a filter block, the convention is to use an
|
Getting all keys or values of hash |
| Overriding hash function Ruby hashes use the methods As with other hash implementations, two objects a and b, will be hashed to the same bucket if In the example below only the instance of class |
| Modifying keys and values You can create a new hash with the keys or values modified, indeed you can also add or delete keys, using inject (opens new window) (AKA, reduce (opens new window)). For example to produce a hash with stringified keys and upper case values: Hash is an enumerable, in essence a collection of key/value pairs. Therefore is has methods such as For every key/value pair in the hash the given block is evaluated, the value of memo on the first run is the seed value passed to To avoid the having to provide the final value, you could use each_with_object (opens new window) instead: Or even map (opens new window): (See this answer (opens new window) for more details, including how to manipulate hashes in place.) |
Set Operations on Hashes SyntaxRemarksHashes in Ruby map keys to values using a hash table. Any hashable object can be used as keys. However, it's very common to use a |
Read Article
https://develop.horje.com/learn/1434/reference