|
1 | 1 | # About |
2 | 2 |
|
| 3 | +[Hashes][hash] or also known as dictionary or map in other languages, is a mutable unsorted collection which maps keys to values. |
| 4 | +Each key is unique and is used to retrieve the corresponding value. |
| 5 | +The keys can be of any data type, including strings, numbers, or even other objects. |
| 6 | + |
| 7 | +Even though hashes are unordered collections, Ruby maintains the insertion order of key-value pairs. |
| 8 | +This means that when you iterate over a hash, the pairs will be returned in the order they were added. |
| 9 | +However, deleting elements may affect the order of remaining elements. |
| 10 | + |
3 | 11 | ## Creating hashes |
4 | 12 |
|
| 13 | +You can create a hash using curly braces `{}` with key-value pairs formed as `key => value` and separated by commas: |
| 14 | + |
| 15 | +```ruby |
| 16 | +my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } |
| 17 | +``` |
| 18 | + |
| 19 | +You can also mix and match different types of keys and values: |
| 20 | + |
| 21 | +```ruby |
| 22 | +my_hash = { 1 => "one", :two => 2, "three" => [3, "three"] } |
| 23 | +``` |
| 24 | + |
| 25 | +Alternatively if the keys are symbols, you can use a more JSON-style syntax: |
| 26 | + |
| 27 | +```ruby |
| 28 | +my_hash = { name: "Alice", age: 30, city: "New York" } |
| 29 | +``` |
| 30 | + |
| 31 | +You can create an empty hash using the `Hash.new` method: |
| 32 | + |
| 33 | +```ruby |
| 34 | +empty_hash = Hash.new |
| 35 | +``` |
| 36 | + |
| 37 | +## Accessing values |
| 38 | + |
| 39 | +You can access values in a hash using their corresponding keys, the syntax reminds of array indexing, but using the key instead of an index: |
5 | 40 |
|
| 41 | +```ruby |
| 42 | +my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } |
| 43 | +my_hash["name"] |
| 44 | +# => "Alice" |
| 45 | +``` |
6 | 46 |
|
7 | | -## Indexing |
| 47 | +If the key does not exist in the hash, it will return `nil`: |
| 48 | + |
| 49 | +```ruby |
| 50 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 51 | +my_hash["city"] |
| 52 | +# => nil |
| 53 | +``` |
| 54 | + |
| 55 | +If the disired behavior is to not return `nil` for non-existing keys, another way of accessing values is by using the [`fetch`][fetch] method, which allows you to provide a default value if the key does not exist. |
| 56 | +If the fetch method is used without a default value and the key does not exist, it will raise a `KeyError` exception. |
| 57 | + |
| 58 | +```ruby |
| 59 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 60 | +my_hash.fetch("city", "Unknown") |
| 61 | +# => "Unknown" |
| 62 | + |
| 63 | +my_hash.fetch("city") |
| 64 | +# => KeyError: key not found: "city" |
| 65 | +``` |
8 | 66 |
|
9 | 67 | ## Modifying |
10 | 68 |
|
| 69 | +You can add or update key-value pairs in a hash by assigning a value to a key using the assignment operator `=`. |
| 70 | +Assigning a value to an existing key will update the value, while assigning a value to a new key will add a new key-value pair: |
| 71 | + |
| 72 | +```ruby |
| 73 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 74 | +my_hash["city"] = "New York" |
| 75 | +my_hash["age"] = 31 |
| 76 | + |
| 77 | +my_hash |
| 78 | +# => { "name" => "Alice", "age" => 31, "city" => "New York" } |
| 79 | +``` |
| 80 | + |
| 81 | +## Default values |
| 82 | + |
| 83 | +When fetching a value for a key that does not exist in the hash, Ruby returns `nil` by default. |
| 84 | + |
| 85 | +```ruby |
| 86 | +my_hash = { "name" => "Alice" } |
| 87 | +my_hash["age"] |
| 88 | +# => nil |
| 89 | +``` |
| 90 | + |
| 91 | +This might not always be desirable, so you can set a default value for the hash using `Hash.new(default_value)`. |
| 92 | +See that the default value is returned only for keys that do not exist in the hash. |
| 93 | + |
| 94 | +```ruby |
| 95 | +my_hash = Hash.new("unknown") |
| 96 | +my_hash["name"] = "Alice" |
| 97 | +my_hash["age"] |
| 98 | +# => "unknown" |
| 99 | + |
| 100 | +my_hash["name"] |
| 101 | +# => "Alice" |
| 102 | +``` |
| 103 | + |
| 104 | +~~~~exercism/caution |
| 105 | +Be cautious when using mutable objects (like arrays or other hashes) as default values, as they can lead to unexpected behavior. |
| 106 | +~~~~ |
| 107 | + |
| 108 | +## Deleting key-value pairs |
| 109 | + |
| 110 | +You can delete a key-value pair from a hash using the [`delete`][delete] method, which takes the key as an argument: |
| 111 | + |
| 112 | +```ruby |
| 113 | +my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } |
| 114 | +my_hash.delete("age") |
| 115 | +my_hash |
| 116 | +# => { "name" => "Alice", "city" => "New York" } |
| 117 | +``` |
| 118 | + |
11 | 119 | ## Methods |
12 | 120 |
|
13 | | -has_value? |
14 | | -include? |
| 121 | +There are several useful methods available for hashes in Ruby. |
| 122 | +Here are some commonly used ones: |
| 123 | + |
| 124 | +### `has_value?` and `include?` |
| 125 | + |
| 126 | +You can check if a hash contains a specific value using the [`has_value?`][has_value?] method. |
| 127 | + |
| 128 | +```ruby |
| 129 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 130 | +my_hash.has_value?(30) |
| 131 | +# => true |
| 132 | +my_hash.has_value?(25) |
| 133 | +# => false |
| 134 | +``` |
| 135 | + |
| 136 | +You can check if a hash contains a specific key using the [`include?`][include?] method. |
| 137 | + |
| 138 | +```ruby |
| 139 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 140 | +my_hash.include?("name") |
| 141 | +# => true |
| 142 | +my_hash.include?("city") |
| 143 | +# => false |
| 144 | +``` |
| 145 | + |
| 146 | +### `keys` and `values` |
| 147 | + |
| 148 | +You can retrieve all the keys of a hash using the [`keys`][keys] method, which returns an array of keys. |
| 149 | + |
| 150 | +```ruby |
| 151 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 152 | +my_hash.keys |
| 153 | +# => ["name", "age"] |
| 154 | +``` |
| 155 | + |
| 156 | +You can retrieve all the values of a hash using the [`values`][values] method, which returns an array of values. |
| 157 | + |
| 158 | +```ruby |
| 159 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 160 | +my_hash.values |
| 161 | +# => ["Alice", 30] |
| 162 | +``` |
| 163 | + |
| 164 | +## Iterating over hashes |
| 165 | + |
| 166 | +You can iterate over the key-value pairs in a hash using the `each_pair` method. |
| 167 | +This will give you access to both the key and the value for each pair: |
| 168 | + |
| 169 | +```ruby |
| 170 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 171 | +my_hash.each_pair do |key, value| |
| 172 | + puts "#{key}: #{value}" |
| 173 | +end |
| 174 | +# Output: |
| 175 | +# name: Alice |
| 176 | +# age: 30 |
| 177 | +``` |
| 178 | + |
| 179 | +You can also iterate over just the keys or just the values using the [`each_key`][each_key] and [`each_value`][each_value] methods, respectively: |
| 180 | + |
| 181 | +```ruby |
| 182 | +my_hash = { "name" => "Alice", "age" => 30 } |
| 183 | +my_hash.each_key do |key| |
| 184 | + puts key |
| 185 | +end |
| 186 | +# Output: |
| 187 | +# name |
| 188 | +# age |
| 189 | +``` |
15 | 190 |
|
16 | | -keys |
17 | | -values |
| 191 | +[each_pair]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-each_pair |
| 192 | +[each_key]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-each_key |
| 193 | +[keys]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-keys |
| 194 | +[each_value]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-each_value |
| 195 | +[values]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-values |
| 196 | +[has_value?]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-has_value-3F |
| 197 | +[include?]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-include-3F |
| 198 | +[hash]: https://docs.ruby-lang.org/en/4.0/Hash.html |
| 199 | +[fetch]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-fetch |
| 200 | +[delete]: https://docs.ruby-lang.org/en/4.0/Hash.html#method-i-delete |
0 commit comments