class Hash[K, out V]
include Enumerable[[K, V]]
An immutable map from keys to values, kept in insertion order.
Iteration, #keys, #values, #to_a and #inspect follow the order in
which the keys were first added. The updating methods (#put, #delete,
#merge ...) return a new hash and leave the receiver as it was;
Hash::Builder is the mutable one. A literal is {a: 1} with Symbol
keys and {"a" => 1} with others. As an Enumerable, the elements are
[key, value] tuples, which a block with two parameters takes apart.
A missing key: #[] raises KeyError, #[]? returns nil and
#fetch takes a default or a block. There is no default value
(Hash.new(0)) and no default_proc.
h = {b: 1, a: 2}
h.put(:c, 3) # => {b: 1, a: 2, c: 3}
h # => {b: 1, a: 2}
h[:c]? # => nil
h[:c] # raises KeyError
h.fetch(:c, 0) # => 0
h.map { |k, v| v } # => [1, 2]
<prelude>:4450
Classes and modules
Hash::BuilderA mutable hash, for building a Hash in place.
Class methods
def self.new -> Hash[K, V]
An empty hash, as Hash[String, Int].new.
<prelude>:4455
def self.build(&: (Hash::Builder[K, V]) -> Void) -> Hash[K, V]
Calls the block with a new Hash::Builder and returns what it holds
afterwards.
Hash[String, Int].build { |b| %w[x yy].each { |s| b[s] = s.size } }
# => {"x" => 1, "yy" => 2}
<prelude>:4463
Methods
def each(&: ([K, V]) -> Void) -> Void
Calls the block with each [key, value] pair, in insertion order; a
block with two parameters takes the pair apart.
<prelude>:4467
def each_pair(&: (K, V) -> Void) -> Void
Calls the block with each key and its value, in insertion order.
<prelude>:4469
def each_key(&: (K) -> Void) -> Void
Calls the block with each key, in insertion order.
<prelude>:4471
def each_value(&: (V) -> Void) -> Void
Calls the block with each value, in the insertion order of the keys.
<prelude>:4473
def [](key: K) -> V
The value for key.
Raises KeyError when there is none: #[]? returns nil and #fetch
takes a default instead.
<prelude>:4478
def []?(key: K) -> V?
The value for key, or nil when there is none. A nil value looks
the same: #key? tells them apart.
<prelude>:4481
def fetch[W >: V](key: K, default: W) -> W
The value for key, or default when there is none.
{a: 1}.fetch(:a, 0) # => 1
{a: 1}.fetch(:b, 0) # => 0
{a: 1}.fetch(:b, "none") # => "none"
<prelude>:4489
def fetch[W >: V](key: K, &: (K) -> W) -> W
The value for key, or the block's result, called with key, when
there is none.
<prelude>:4492
def dig(key: K) -> V?
The value for key, or nil when there is none, as #[]?. It takes
one key only.
<prelude>:4495
def key?(key: K) -> Bool
Whether key is a key.
<prelude>:4497
def has_key?(key: K) -> Bool
Whether key is a key: the same as #key?.
<prelude>:4499
def include?(key: Object) -> Bool
Whether key is a key (unlike Enumerable#include?, which would compare
pairs). A value of another type than the keys' is none.
<prelude>:4502
def member?(key: Object) -> Bool
Whether key is a key: the same as #include?.
<prelude>:4504
def value?(value: Object) -> Bool
Whether some key has value, compared with ==.
<prelude>:4506
def has_value?(value: Object) -> Bool
Whether some key has value: the same as #value?.
<prelude>:4508
def key(value: Object) -> K?
The first key, in insertion order, whose value is value, or nil.
<prelude>:4510
def values_at(*keys: K) -> Array[V?]
The values for keys, in their order, with nil for a missing key.
<prelude>:4512
def fetch_values(*keys: K) -> Array[V]
The values for keys, in their order. Raises KeyError for a missing
key.
<prelude>:4515
def compact -> Hash[K, V]
The hash without the pairs whose value is nil.
<prelude>:4517
def except(*keys: K) -> Hash[K, V]
The hash without keys; a key it does not have is ignored.
<prelude>:4519
def slice(*keys: K) -> Hash[K, V]
The pairs for those of keys that are present, in the order of keys.
{a: 1, b: 2, c: 3}.slice(:c, :a, :z) # => {c: 3, a: 1}
<prelude>:4525
def filter(&: (K, V) -> Object) -> Hash[K, V]
The pairs the block is truthy for: the same as #select.
<prelude>:4527
def delete_if(&: (K, V) -> Object) -> Hash[K, V]
The hash without the pairs the block is truthy for: the same as
#reject. The receiver is not changed, unlike Ruby's.
<prelude>:4530
def keep_if(&: (K, V) -> Object) -> Hash[K, V]
The pairs the block is truthy for: the same as #select. The receiver
is not changed, unlike Ruby's.
<prelude>:4533
def keys -> Array[K]
The keys, in insertion order.
<prelude>:4535
def values -> Array[V]
The values, in the insertion order of their keys.
<prelude>:4537
def size -> Int
The number of pairs.
<prelude>:4539
def length -> Int
The number of pairs: the same as #size.
<prelude>:4541
def empty? -> Bool
Whether there is no pair.
<prelude>:4543
def put[W >: V](key: K, value: W) -> Hash[K, W]
The hash with key mapped to value.
A new key comes last; a key already there keeps its place and takes the new value.
h = {a: 1, b: 2}
h.put(:a, 9) # => {a: 9, b: 2}
h.put(:c, 3) # => {a: 1, b: 2, c: 3}
h.delete(:a).put(:a, 1) # => {b: 2, a: 1}
<prelude>:4555
def delete(key: K) -> Hash[K, V]
The hash without key, or the same pairs when it has no such key.
Unlike Ruby's, it returns the hash, not the value.
<prelude>:4558
def merge[W >: V](other: Hash[K, W]) -> Hash[K, W]
The hash with the pairs of other added: for a key in both, the value
of other, at the place the key had; the new keys follow, in the order
of other.
{a: 1, b: 2}.merge({c: 3, a: 0}) # => {a: 0, b: 2, c: 3}
<prelude>:4566
def merge[W >: V](other: Hash[K, W], &: (K, W, W) -> W) -> Hash[K, W]
The hash with the pairs of other added, as #merge without a block,
but for a key in both, the block's result, called with the key, the old
value and the value of other.
{a: 1, b: 2}.merge({a: 10}) { |k, old, new| old + new }
# => {a: 11, b: 2}
<prelude>:4575
def update[W >: V](key: K, &: (V?) -> W) -> Hash[K, W]
The hash with key mapped to the block's result, called with the value
for key, or nil when there is none. A new key comes last.
counts = {"a" => 1}
counts.update("a") { |n| (n || 0) + 1 } # => {"a" => 2}
counts.update("b") { |n| (n || 0) + 1 } # => {"a" => 1, "b" => 1}
<prelude>:4584
def select(&: (K, V) -> Object) -> Hash[K, V]
The pairs the block, called with each key and value, is truthy for, in order.
<prelude>:4587
def reject(&: (K, V) -> Object) -> Hash[K, V]
The hash without the pairs the block, called with each key and value, is truthy for.
<prelude>:4590
def transform_values[U](&: (V) -> U) -> Hash[K, U]
The hash with each value replaced by the block's result for it.
<prelude>:4592
def transform_keys[U](&: (K) -> U) -> Hash[U, V]
The hash with each key replaced by the block's result for it.
When two keys become one, the later pair's value is kept, at the earlier one's place.
{a: 1, b: 2}.transform_keys { |k| k.to_s } # => {"a" => 1, "b" => 2}
{a: 1, b: 2}.transform_keys { |k| 0 } # => {0 => 2}
<prelude>:4602
def invert[W >: V] -> Hash[W, K]
The hash with keys and values swapped. When several keys have the same value, the last of them is kept.
<prelude>:4605
def to_a -> Array[[K, V]]
The [key, value] pairs, in insertion order.
<prelude>:4607
def to_h -> Hash[K, V]
The hash itself. With a block, to_h is Enumerable#to_h.
<prelude>:4609
def to_s -> String
The literal form, as #inspect.
<prelude>:4611
def inspect -> String
The literal form, in insertion order: {a: 1} for Symbol keys,
{"a" => 1} for others, and {} when empty.
<prelude>:4614
def ==(other: Object) -> Bool
Whether other is a hash with the same keys mapped to equal values,
whatever their order.
{a: 1, b: 2} == {b: 2, a: 1} # => true
{a: 1, b: 2}.to_a == {b: 2, a: 1}.to_a # => false
<prelude>:4622
def hash -> Int
A hash code consistent with #==, so the same whatever the order of the
keys.
<prelude>:4625