class

Immutable::Map(K, V)

Inherits Enumerable < Reference < Object

Constructors

new(hash : Hash(K, V) = {} of K => V)

Creates a map with the given key-values

m = Immutable::Map.new({:a => 1, :b => true}) # Map {:a => 1, :b => true}
Source
new(hash : Hash(K, V) = {} of K => V, &block : K -> V)

Creates a map with the given key-values. When getting a key-value that is not in the map, the given block is executed passing the key, and the return value is returned.

m = Immutable::Map.new({:a => 123, :b => 321 }) # Map {:a => 123, :b => 321}
Source
new(e : Enumerable(Tuple(_, _)))

Creates a map with the given key-values.

m = Immutable::Map.new([{:a, 123}, {:b, 321}]) # Map {:a => 123, :b => 321}
Source

Class methods

[](hash : Hash(K, V) = {} of K => V)

Creates a map with the given key-values

m = Immutable::Map[{:a => 123, :b => 321}] # Map {:a => 123, :b => 321}
Source

Instance methods

==(other : Map)
Source
[](key : K)

Returns the value associated with the given key, if existing, else raises KeyError. See also fetch

Source
[]?(key : K)

Returns the value associated with the given key, if existing, else nil

Source
delete(key : K)

Returns a modified copy of the map with the key-value pair removed. If the key is not existing, it raises KeyError

m  = Immutable::Map[{:foo => 123, :bar => 321 }]
m2 = m.delete(:bar) # => Map {:foo => 123}
m                   # => Map {:foo => 123, bar: 321}
Source
each

Calls the given block for each key-value and passes in a tuple of key and value. The order of iteration is not specified.

m = Immutable::Map[{"foo" => "bar"}]
m.each do |keyval|
  keyval[0] # => "foo"
  keyval[1] # => "bar"
end
Source
each

Returns an iterator over the map entries, returning a Tuple of the key and value. The order of iteration is not specified.

map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each

entry = iterator.next
entry[0] # => "foo"
entry[1] # => "bar"

entry = iterator.next
entry[0] # => "baz"
entry[1] # => "qux"
Source
each_key

Calls the given block for each key-value pair and passes in the key.

m = Immutable::Map[{"foo" => "bar"}]
m.each_key do |key|
  key # => "foo"
end
Source
each_key

Returns an iterator over the map keys. The order is not guaranteed.

map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each_key

key = iterator.next
key # => "foo"

key = iterator.next
key # => "baz"
Source
each_value

Calls the given block for each key-value pair and passes in the value.

m = Immutable::Map[{"foo" => "bar"}]
m.each_value do |val|
  val # => "bar"
end
Source
each_value

Returns an iterator over the map values. The order is not specified.

map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each_value

val = iterator.next
val # => "bar"

val = iterator.next
val # => "qux"
Source
fetch(key : K, default)

Returns the value associated with the given key, if existing, else it returns the provided default value

Source
fetch(key : K)

Returns the value associated with the given key, if existing, else raises KeyError

Source
fetch(key : K, &block : K -> _)

Returns the value associated with the given key, if existing, else executes the given block and returns its value

Source
hash

See Object#hash.

map = Immutable::Map[{"foo" => "bar"}]
map.hash # => 63502
Source
inspect(io : IO)

Appends a String representation of this object to the given IO object.

Source
keys

Returns only the keys as an Array. The order is not specified.

m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.keys # => ["foo", "bar"]
Source
merge(hash : Hash(K, V))

Returns a new map with the keys and values of this map and the given hash combined. A value in the given hash takes precedence over the one in this map.

map = Immutable::Map[{"foo" => "bar"}]
merged = map.merge({"baz" => "qux"})
merged # => Map {"foo" => "bar", "baz" => "qux"}
map    # => Map {"foo" => "bar"}
Source
merge(map : Map(K, V))

Returns a new map with the keys and values of this map and the given map combined. A value in the given map takes precedence over the one in this map.

map = Immutable::Map[{"foo" => "bar"}]
merged = map.merge(Immutable::Map[{"baz" => "qux"}])
merged # => Map {"foo" => "bar", "baz" => "qux"}
map    # => Map {"foo" => "bar"}
Source
merge(hash : Hash(L, W)) forall L, W
Source
merge(map : Map(L, W)) forall L, W
Source
set(key : K, value : V)

Returns a modified copy of the map where key is associated to value

m  = Immutable::Map[{:foo => 123}]
m2 = m.set(:bar, 321) # => Map {:foo => 123, :bar => 321}
m                     # => Map {:foo => 123}
Source
size

Returns the number of key-value pairs in the map

Source
to_json(json : JSON::Builder)

Returns the JSON serialization of this map

Source
to_s(io : IO)

Appends a String representation of this map to the given IO object.

Source
transient

Executes the given block passing a transient version of the map, then converts the transient map back to an immutable one and returns it.

This is useful to perform several updates on a map in an efficient way: as the transient map supports the same API of map, but performs updates in place, avoiding unnecessary object allocations.

map = Immutable::Map(Int32, Int32).new
m2 = map.transient do |m|
  100.times { |i| m = m.set(i, i * 2) }
end
m2.size # => 100

Note that, as the transient is mutable, it is not thread-safe.

Source
values

Returns only the values as an Array. The order is not specified.

m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.values # => ["bar", "qux"]
Source

Nested types