| 1 |
class Hash |
| 2 |
|
| 3 |
# ISO does not define Hash#each_pair, so each_pair is defined in gem. |
| 4 |
alias each_pair each |
| 5 |
|
| 6 |
## |
| 7 |
# call-seq: |
| 8 |
# Hash[ key, value, ... ] -> new_hash |
| 9 |
# Hash[ [ [key, value], ... ] ] -> new_hash |
| 10 |
# Hash[ object ] -> new_hash |
| 11 |
# |
| 12 |
# Creates a new hash populated with the given objects. |
| 13 |
# |
| 14 |
# Similar to the literal `{ _key_ => _value_, ... }`. In the first |
| 15 |
# form, keys and values occur in pairs, so there must be an even number of |
| 16 |
# arguments. |
| 17 |
# |
| 18 |
# The second and third form take a single argument which is either an array |
| 19 |
# of key-value pairs or an object convertible to a hash. |
| 20 |
# |
| 21 |
# Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200} |
| 22 |
# Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200} |
| 23 |
# Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200} |
| 24 |
# |
| 25 |
|
| 26 |
def self.[](*object) |
| 27 |
length = object.length |
| 28 |
if length == 1 |
| 29 |
o = object[0] |
| 30 |
if Hash === o |
| 31 |
h = self.new |
| 32 |
o.each { |k, v| h[k] = v } |
| 33 |
return h |
| 34 |
elsif o.respond_to?(:to_a) |
| 35 |
h = self.new |
| 36 |
o.to_a.each do |i| |
| 37 |
raise ArgumentError, "wrong element type #{i.class} (expected array)" unless i.respond_to?(:to_a) |
| 38 |
k, v = nil |
| 39 |
case i.size |
| 40 |
when 2 |
| 41 |
k = i[0] |
| 42 |
v = i[1] |
| 43 |
when 1 |
| 44 |
k = i[0] |
| 45 |
else |
| 46 |
raise ArgumentError, "invalid number of elements (#{i.size} for 1..2)" |
| 47 |
end |
| 48 |
h[k] = v |
| 49 |
end |
| 50 |
return h |
| 51 |
end |
| 52 |
end |
| 53 |
unless length % 2 == 0 |
| 54 |
raise ArgumentError, 'odd number of arguments for Hash' |
| 55 |
end |
| 56 |
h = self.new |
| 57 |
0.step(length - 2, 2) do |i| |
| 58 |
h[object[i]] = object[i + 1] |
| 59 |
end |
| 60 |
h |
| 61 |
end |
| 62 |
|
| 63 |
## |
| 64 |
# call-seq: |
| 65 |
# hsh.merge!(other_hash..) -> hsh |
| 66 |
# hsh.merge!(other_hash..){|key, oldval, newval| block} -> hsh |
| 67 |
# |
| 68 |
# Adds the contents of _other_hash_ to _hsh_. If no block is specified, |
| 69 |
# entries with duplicate keys are overwritten with the values from |
| 70 |
# _other_hash_, otherwise the value of each duplicate key is determined by |
| 71 |
# calling the block with the key, its value in _hsh_ and its value in |
| 72 |
# _other_hash_. |
| 73 |
# |
| 74 |
# h1 = { "a" => 100, "b" => 200 } |
| 75 |
# h2 = { "b" => 254, "c" => 300 } |
| 76 |
# h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300} |
| 77 |
# |
| 78 |
# h1 = { "a" => 100, "b" => 200 } |
| 79 |
# h2 = { "b" => 254, "c" => 300 } |
| 80 |
# h1.merge!(h2) { |key, v1, v2| v1 } |
| 81 |
# #=> {"a"=>100, "b"=>200, "c"=>300} |
| 82 |
# |
| 83 |
|
| 84 |
def merge!(*others, &block) |
| 85 |
i = 0; len=others.size |
| 86 |
return self.__merge(*others) unless block |
| 87 |
while i<len |
| 88 |
other = others[i] |
| 89 |
i += 1 |
| 90 |
raise TypeError, "Hash required (#{other.class} given)" unless Hash === other |
| 91 |
if block |
| 92 |
other.each_key{|k| |
| 93 |
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k] |
| 94 |
} |
| 95 |
else |
| 96 |
other.each_key{|k| self[k] = other[k]} |
| 97 |
end |
| 98 |
end |
| 99 |
self |
| 100 |
end |
| 101 |
|
| 102 |
alias update merge! |
| 103 |
|
| 104 |
## |
| 105 |
# call-seq: |
| 106 |
# hsh.compact! -> hsh |
| 107 |
# |
| 108 |
# Removes all nil values from the hash. Returns the hash. |
| 109 |
# Returns nil if the hash does not contain nil values. |
| 110 |
# |
| 111 |
# h = { a: 1, b: false, c: nil } |
| 112 |
# h.compact! #=> { a: 1, b: false } |
| 113 |
# |
| 114 |
|
| 115 |
def compact! |
| 116 |
self.__compact |
| 117 |
end |
| 118 |
|
| 119 |
## |
| 120 |
# call-seq: |
| 121 |
# hsh.compact -> new_hsh |
| 122 |
# |
| 123 |
# Returns a new hash with the nil values/key pairs removed |
| 124 |
# |
| 125 |
# h = { a: 1, b: false, c: nil } |
| 126 |
# h.compact #=> { a: 1, b: false } |
| 127 |
# h #=> { a: 1, b: false, c: nil } |
| 128 |
# |
| 129 |
def compact |
| 130 |
h=self.dup |
| 131 |
h.__compact |
| 132 |
h |
| 133 |
end |
| 134 |
|
| 135 |
## |
| 136 |
# call-seq: |
| 137 |
# hsh.fetch(key [, default] ) -> obj |
| 138 |
# hsh.fetch(key) {| key | block } -> obj |
| 139 |
# |
| 140 |
# Returns a value from the hash for the given key. If the key can't be |
| 141 |
# found, there are several options: With no other arguments, it will |
| 142 |
# raise an <code>KeyError</code> exception; if <i>default</i> is |
| 143 |
# given, then that will be returned; if the optional code block is |
| 144 |
# specified, then that will be run and its result returned. |
| 145 |
# |
| 146 |
# h = { "a" => 100, "b" => 200 } |
| 147 |
# h.fetch("a") #=> 100 |
| 148 |
# h.fetch("z", "go fish") #=> "go fish" |
| 149 |
# h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z" |
| 150 |
# |
| 151 |
# The following example shows that an exception is raised if the key |
| 152 |
# is not found and a default value is not supplied. |
| 153 |
# |
| 154 |
# h = { "a" => 100, "b" => 200 } |
| 155 |
# h.fetch("z") |
| 156 |
# |
| 157 |
# <em>produces:</em> |
| 158 |
# |
| 159 |
# prog.rb:2:in 'fetch': key not found (KeyError) |
| 160 |
# from prog.rb:2 |
| 161 |
# |
| 162 |
|
| 163 |
def fetch(key, none=NONE, &block) |
| 164 |
unless self.key?(key) |
| 165 |
if block |
| 166 |
block.call(key) |
| 167 |
elsif none != NONE |
| 168 |
none |
| 169 |
else |
| 170 |
raise KeyError, "Key not found: #{key.inspect}" |
| 171 |
end |
| 172 |
else |
| 173 |
self[key] |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
## |
| 178 |
# call-seq: |
| 179 |
# hsh.delete_if {| key, value | block } -> hsh |
| 180 |
# hsh.delete_if -> an_enumerator |
| 181 |
# |
| 182 |
# Deletes every key-value pair from <i>hsh</i> for which <i>block</i> |
| 183 |
# evaluates to <code>true</code>. |
| 184 |
# |
| 185 |
# If no block is given, an enumerator is returned instead. |
| 186 |
# |
| 187 |
# h = { "a" => 100, "b" => 200, "c" => 300 } |
| 188 |
# h.delete_if {|key, value| key >= "b" } #=> {"a"=>100} |
| 189 |
# |
| 190 |
|
| 191 |
def delete_if(&block) |
| 192 |
return to_enum :delete_if unless block |
| 193 |
|
| 194 |
self.each do |k, v| |
| 195 |
self.delete(k) if block.call(k, v) |
| 196 |
end |
| 197 |
self |
| 198 |
end |
| 199 |
|
| 200 |
## |
| 201 |
# call-seq: |
| 202 |
# hash.flatten -> an_array |
| 203 |
# hash.flatten(level) -> an_array |
| 204 |
# |
| 205 |
# Returns a new array that is a one-dimensional flattening of this |
| 206 |
# hash. That is, for every key or value that is an array, extract |
| 207 |
# its elements into the new array. Unlike Array#flatten, this |
| 208 |
# method does not flatten recursively by default. The optional |
| 209 |
# <i>level</i> argument determines the level of recursion to flatten. |
| 210 |
# |
| 211 |
# a = {1=> "one", 2 => [2,"two"], 3 => "three"} |
| 212 |
# a.flatten # => [1, "one", 2, [2, "two"], 3, "three"] |
| 213 |
# a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"] |
| 214 |
# |
| 215 |
|
| 216 |
def flatten(level=1) |
| 217 |
self.to_a.flatten(level) |
| 218 |
end |
| 219 |
|
| 220 |
## |
| 221 |
# call-seq: |
| 222 |
# hsh.invert -> new_hash |
| 223 |
# |
| 224 |
# Returns a new hash created by using <i>hsh</i>'s values as keys, and |
| 225 |
# the keys as values. |
| 226 |
# |
| 227 |
# h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } |
| 228 |
# h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"} |
| 229 |
# |
| 230 |
|
| 231 |
def invert |
| 232 |
h = self.class.new |
| 233 |
self.each {|k, v| h[v] = k } |
| 234 |
h |
| 235 |
end |
| 236 |
|
| 237 |
## |
| 238 |
# call-seq: |
| 239 |
# hsh.keep_if {| key, value | block } -> hsh |
| 240 |
# hsh.keep_if -> an_enumerator |
| 241 |
# |
| 242 |
# Deletes every key-value pair from <i>hsh</i> for which <i>block</i> |
| 243 |
# evaluates to false. |
| 244 |
# |
| 245 |
# If no block is given, an enumerator is returned instead. |
| 246 |
# |
| 247 |
|
| 248 |
def keep_if(&block) |
| 249 |
return to_enum :keep_if unless block |
| 250 |
|
| 251 |
self.each do |k, v| |
| 252 |
unless block.call([k, v]) |
| 253 |
self.delete(k) |
| 254 |
end |
| 255 |
end |
| 256 |
self |
| 257 |
end |
| 258 |
|
| 259 |
## |
| 260 |
# call-seq: |
| 261 |
# hsh.key(value) -> key |
| 262 |
# |
| 263 |
# Returns the key of an occurrence of a given value. If the value is |
| 264 |
# not found, returns <code>nil</code>. |
| 265 |
# |
| 266 |
# h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 } |
| 267 |
# h.key(200) #=> "b" |
| 268 |
# h.key(300) #=> "c" |
| 269 |
# h.key(999) #=> nil |
| 270 |
# |
| 271 |
|
| 272 |
def key(val) |
| 273 |
self.each do |k, v| |
| 274 |
return k if v == val |
| 275 |
end |
| 276 |
nil |
| 277 |
end |
| 278 |
|
| 279 |
## |
| 280 |
# call-seq: |
| 281 |
# hsh.to_h -> hsh or new_hash |
| 282 |
# |
| 283 |
# Returns +self+. If called on a subclass of Hash, converts |
| 284 |
# the receiver to a Hash object. |
| 285 |
# |
| 286 |
def to_h |
| 287 |
self |
| 288 |
end |
| 289 |
|
| 290 |
## |
| 291 |
# call-seq: |
| 292 |
# hash < other -> true or false |
| 293 |
# |
| 294 |
# Returns <code>true</code> if <i>hash</i> is subset of |
| 295 |
# <i>other</i>. |
| 296 |
# |
| 297 |
# h1 = {a:1, b:2} |
| 298 |
# h2 = {a:1, b:2, c:3} |
| 299 |
# h1 < h2 #=> true |
| 300 |
# h2 < h1 #=> false |
| 301 |
# h1 < h1 #=> false |
| 302 |
# |
| 303 |
def <(hash) |
| 304 |
raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash |
| 305 |
size < hash.size and all? {|key, val| |
| 306 |
hash.key?(key) and hash[key] == val |
| 307 |
} |
| 308 |
end |
| 309 |
|
| 310 |
## |
| 311 |
# call-seq: |
| 312 |
# hash <= other -> true or false |
| 313 |
# |
| 314 |
# Returns <code>true</code> if <i>hash</i> is subset of |
| 315 |
# <i>other</i> or equals to <i>other</i>. |
| 316 |
# |
| 317 |
# h1 = {a:1, b:2} |
| 318 |
# h2 = {a:1, b:2, c:3} |
| 319 |
# h1 <= h2 #=> true |
| 320 |
# h2 <= h1 #=> false |
| 321 |
# h1 <= h1 #=> true |
| 322 |
# |
| 323 |
def <=(hash) |
| 324 |
raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash |
| 325 |
size <= hash.size and all? {|key, val| |
| 326 |
hash.key?(key) and hash[key] == val |
| 327 |
} |
| 328 |
end |
| 329 |
|
| 330 |
## |
| 331 |
# call-seq: |
| 332 |
# hash > other -> true or false |
| 333 |
# |
| 334 |
# Returns <code>true</code> if <i>other</i> is subset of |
| 335 |
# <i>hash</i>. |
| 336 |
# |
| 337 |
# h1 = {a:1, b:2} |
| 338 |
# h2 = {a:1, b:2, c:3} |
| 339 |
# h1 > h2 #=> false |
| 340 |
# h2 > h1 #=> true |
| 341 |
# h1 > h1 #=> false |
| 342 |
# |
| 343 |
def >(hash) |
| 344 |
raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash |
| 345 |
size > hash.size and hash.all? {|key, val| |
| 346 |
key?(key) and self[key] == val |
| 347 |
} |
| 348 |
end |
| 349 |
|
| 350 |
## |
| 351 |
# call-seq: |
| 352 |
# hash >= other -> true or false |
| 353 |
# |
| 354 |
# Returns <code>true</code> if <i>other</i> is subset of |
| 355 |
# <i>hash</i> or equals to <i>hash</i>. |
| 356 |
# |
| 357 |
# h1 = {a:1, b:2} |
| 358 |
# h2 = {a:1, b:2, c:3} |
| 359 |
# h1 >= h2 #=> false |
| 360 |
# h2 >= h1 #=> true |
| 361 |
# h1 >= h1 #=> true |
| 362 |
# |
| 363 |
def >=(hash) |
| 364 |
raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash |
| 365 |
size >= hash.size and hash.all? {|key, val| |
| 366 |
key?(key) and self[key] == val |
| 367 |
} |
| 368 |
end |
| 369 |
|
| 370 |
## |
| 371 |
# call-seq: |
| 372 |
# hsh.dig(key,...) -> object |
| 373 |
# |
| 374 |
# Extracts the nested value specified by the sequence of <i>key</i> |
| 375 |
# objects by calling +dig+ at each step, returning +nil+ if any |
| 376 |
# intermediate step is +nil+. |
| 377 |
# |
| 378 |
def dig(idx,*args) |
| 379 |
n = self[idx] |
| 380 |
if args.size > 0 |
| 381 |
n&.dig(*args) |
| 382 |
else |
| 383 |
n |
| 384 |
end |
| 385 |
end |
| 386 |
|
| 387 |
## |
| 388 |
# call-seq: |
| 389 |
# hsh.transform_keys {|key| block } -> new_hash |
| 390 |
# hsh.transform_keys -> an_enumerator |
| 391 |
# |
| 392 |
# Returns a new hash, with the keys computed from running the block |
| 393 |
# once for each key in the hash, and the values unchanged. |
| 394 |
# |
| 395 |
# If no block is given, an enumerator is returned instead. |
| 396 |
# |
| 397 |
def transform_keys(&block) |
| 398 |
return to_enum :transform_keys unless block |
| 399 |
hash = {} |
| 400 |
self.keys.each do |k| |
| 401 |
new_key = block.call(k) |
| 402 |
hash[new_key] = self[k] |
| 403 |
end |
| 404 |
hash |
| 405 |
end |
| 406 |
## |
| 407 |
# call-seq: |
| 408 |
# hsh.transform_keys! {|key| block } -> hsh |
| 409 |
# hsh.transform_keys! -> an_enumerator |
| 410 |
# |
| 411 |
# Invokes the given block once for each key in <i>hsh</i>, replacing it |
| 412 |
# with the new key returned by the block, and then returns <i>hsh</i>. |
| 413 |
# |
| 414 |
# If no block is given, an enumerator is returned instead. |
| 415 |
# |
| 416 |
def transform_keys!(&block) |
| 417 |
return to_enum :transform_keys! unless block |
| 418 |
hash = self.transform_keys(&block) |
| 419 |
self.replace(hash) |
| 420 |
self |
| 421 |
end |
| 422 |
## |
| 423 |
# call-seq: |
| 424 |
# hsh.transform_values {|value| block } -> new_hash |
| 425 |
# hsh.transform_values -> an_enumerator |
| 426 |
# |
| 427 |
# Returns a new hash with the results of running the block once for |
| 428 |
# every value. |
| 429 |
# This method does not change the keys. |
| 430 |
# |
| 431 |
# If no block is given, an enumerator is returned instead. |
| 432 |
# |
| 433 |
def transform_values(&b) |
| 434 |
return to_enum :transform_values unless block_given? |
| 435 |
hash = {} |
| 436 |
self.keys.each do |k| |
| 437 |
hash[k] = yield(self[k]) |
| 438 |
end |
| 439 |
hash |
| 440 |
end |
| 441 |
|
| 442 |
## |
| 443 |
# call-seq: |
| 444 |
# hsh.transform_values! {|key| block } -> hsh |
| 445 |
# hsh.transform_values! -> an_enumerator |
| 446 |
# |
| 447 |
# Invokes the given block once for each value in the hash, replacing |
| 448 |
# with the new value returned by the block, and then returns <i>hsh</i>. |
| 449 |
# |
| 450 |
# If no block is given, an enumerator is returned instead. |
| 451 |
# |
| 452 |
def transform_values!(&b) |
| 453 |
return to_enum :transform_values! unless block_given? |
| 454 |
self.keys.each do |k| |
| 455 |
self[k] = yield(self[k]) |
| 456 |
end |
| 457 |
self |
| 458 |
end |
| 459 |
|
| 460 |
def to_proc |
| 461 |
->x{self[x]} |
| 462 |
end |
| 463 |
|
| 464 |
## |
| 465 |
# call-seq: |
| 466 |
# hsh.fetch_values(key, ...) -> array |
| 467 |
# hsh.fetch_values(key, ...) { |key| block } -> array |
| 468 |
# |
| 469 |
# Returns an array containing the values associated with the given keys |
| 470 |
# but also raises <code>KeyError</code> when one of keys can't be found. |
| 471 |
# Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>. |
| 472 |
# |
| 473 |
# h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } |
| 474 |
# |
| 475 |
# h.fetch_values("cow", "cat") #=> ["bovine", "feline"] |
| 476 |
# h.fetch_values("cow", "bird") # raises KeyError |
| 477 |
# h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"] |
| 478 |
# |
| 479 |
def fetch_values(*keys, &block) |
| 480 |
keys.map do |k| |
| 481 |
self.fetch(k, &block) |
| 482 |
end |
| 483 |
end |
| 484 |
|
| 485 |
alias filter select |
| 486 |
alias filter! select! |
| 487 |
end |