class Hash::Builder[K, V]

include Enumerable[[K, V]]

A mutable hash, for building a Hash in place.

It keeps insertion order as Hash does: a new key comes last, a key already there keeps its place, and a key deleted and stored again comes last. It is not Shareable. #to_h (and #freeze) hands the pairs over as a Hash without copying and leaves the builder empty, as Array::Builder#to_a does; #snapshot copies. A missing key is as in Hash: #[] raises KeyError, #[]? returns nil, #fetch takes a default.

counts = Hash::Builder[String, Int].new
%w[b a b].each { |w| counts.update(w) { |n| (n || 0) + 1 } }
counts["b"]              # => 2
counts.to_h              # => {"b" => 2, "a" => 1}
counts.empty?            # => true

<prelude>:4644

Class methods

def self.new -> Hash::Builder[K, V]

An empty builder, as Hash::Builder[String, Int].new.

<prelude>:4649

Methods

def [](key: K) -> V

The value for key. Raises KeyError when there is none: #[]? returns nil and #fetch takes a default instead.

<prelude>:4652

def []?(key: K) -> V?

The value for key, or nil when there is none.

<prelude>:4654

def []=(key: K, value: V) -> V

Maps key to value and returns value. A new key comes last; a key already there keeps its place.

<prelude>:4657

def store(key: K, value: V) -> V

Maps key to value and returns value: the same as #[]=.

<prelude>:4659

def fetch(key: K, default: V) -> V

The value for key, or default when there is none. Nothing is stored.

<prelude>:4661

def delete(key: K) -> V?

Removes key and returns its value, or nil when there was none.

<prelude>:4663

def key?(key: K) -> Bool

Whether key is a key.

<prelude>:4665

def keys -> Array[K]

The keys, in insertion order.

<prelude>:4667

def values -> Array[V]

The values, in the insertion order of their keys.

<prelude>:4669

def size -> Int

The number of pairs.

<prelude>:4671

def empty? -> Bool

Whether there is no pair.

<prelude>:4673

def clear -> Self

Removes every pair and returns the builder.

<prelude>:4675

def merge!(other: Hash[K, V]) -> Self

Stores the pairs of other, in its order, and returns the builder. A key already there keeps its place and takes the value of other.

<prelude>:4678

def merge!(other: Hash[K, V], &: (K, V, V) -> V) -> Self

Stores the pairs of other, as #merge! without a block, but for a key already there, the block's result, called with the key, the old value and the value of other. Returns the builder.

<prelude>:4682

def update(key: K, &: (V?) -> V) -> Self

Maps key to the block's result, called with the value for key or nil when there is none, and returns the builder.

<prelude>:4685

def each(&: ([K, V]) -> Void) -> Void

Calls the block with each [key, value] pair, in insertion order.

<prelude>:4687

def each_pair(&: (K, V) -> Void) -> Void

Calls the block with each key and its value, in insertion order.

<prelude>:4689

def each_key(&: (K) -> Void) -> Void

Calls the block with each key, in insertion order.

<prelude>:4691

def each_value(&: (V) -> Void) -> Void

Calls the block with each value, in the insertion order of the keys.

<prelude>:4693

def fetch(key: K, &: (K) -> V) -> V

The value for key, or the block's result, called with key, when there is none. Nothing is stored.

<prelude>:4696

def has_key?(key: K) -> Bool

Whether key is a key: the same as #key?.

<prelude>:4698

def include?(key: Object) -> Bool

Whether key is a key. A value of another type than the keys' is none.

<prelude>:4700

def value?(value: V) -> Bool

Whether some key has value, compared with ==.

<prelude>:4702

def length -> Int

The number of pairs: the same as #size.

<prelude>:4704

def select!(&: (K, V) -> Object) -> Self

Keeps only the pairs the block is truthy for and returns the builder (even when nothing changed, unlike Ruby's nil).

<prelude>:4707

def reject!(&: (K, V) -> Object) -> Self

Removes the pairs the block is truthy for and returns the builder (even when nothing changed, unlike Ruby's nil).

<prelude>:4710

def delete_if(&: (K, V) -> Object) -> Self

Removes the pairs the block is truthy for and returns the builder: the same as #reject!.

<prelude>:4713

def keep_if(&: (K, V) -> Object) -> Self

Keeps only the pairs the block is truthy for and returns the builder: the same as #select!.

<prelude>:4716

def transform_values!(&: (V) -> V) -> Self

Replaces each value by the block's result for it and returns the builder.

<prelude>:4719

def compact! -> Self

Removes the pairs whose value is nil and returns the builder.

<prelude>:4721

def replace(other: Hash[K, V]) -> Self

Replaces the pairs by those of other and returns the builder.

<prelude>:4723

def freeze -> Hash[K, V]

The pairs as a Hash, leaving the builder empty: the same as #to_h.

<prelude>:4725

def to_h -> Hash[K, V]

The pairs as a Hash, handed over without copying: the builder is left empty, and can be filled again.

<prelude>:4728

def snapshot -> Hash[K, V]

A copy of the pairs as a Hash; the builder keeps them.

b = Hash::Builder[Symbol, Int].new
b[:a] = 1
b.snapshot               # => {a: 1}
b.size                   # => 1

<prelude>:4737

def to_s -> String

The literal form of the pairs, as Hash#inspect.

<prelude>:4739

def inspect -> String

The literal form of the pairs, as Hash#inspect.

<prelude>:4741