RMap.Ruby (REnum v0.8.0)

Summarized all of Ruby's Hash functions. Functions corresponding to the following patterns are not implemented

  • When a function with the same name already exists in Elixir.
  • When a method name includes !.
  • <, <=, ==, >, >=, [], []=, default_*

Link to this section Summary

Functions

Returns a 2-element tuple containing a given key and its value.

Returns %{}.

Returns the object in nested map that is specified by a given key and additional arguments.

Calls the function with each key; returns :ok.

Calls the function with each value; returns :ok.

Returns a map excluding entries for the given keys.

Returns a list containing the values associated with the given keys.

When a function is given, calls the function with each missing key, treating the block's return value as the value for that key.

Returns a list whose entries are those for which the function returns a truthy value.

Returns a flatten list.

Returns a map object with the each key-value pair inverted.

Returns a 2-element tuple consisting of the key and value of the first-found entry having a given value.

Returns a list whose entries are all those from self for which the function returns false or nil.

Removes the first map entry; returns a 2-element tuple. First element is {key, value}. Second element is a map without first pair.

Returns given map.

Returns a map with modified keys.

Returns a map with modified values.

Returns true if value is a value in list, otherwise false.

Returns a list containing values for the given keys.

Link to this section Functions

Link to this function

assoc(map, key)

Specs

assoc(map(), any()) :: any()

Returns a 2-element tuple containing a given key and its value.

Examples

iex> RMap.assoc(%{a: 1, b: 2, c: 3}, :a)
{:a, 1}

iex> RMap.assoc(%{a: 1, b: 2, c: 3}, :d)
nil

iex> RMap.assoc(%{a: %{b: %{c: 1}}}, :a)
{:a, %{b: %{c: 1}}}

Specs

clear(map()) :: %{}

Returns %{}.

Examples

iex> RMap.clear(%{a: 1, b: 2, c: 3})
%{}
Link to this function

delete_if(map, func)

See RMap.Ruby.reject/2.

Link to this function

dig(result, keys)

Specs

dig(map(), list()) :: any()

Returns the object in nested map that is specified by a given key and additional arguments.

Examples

iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :b, :c])
1

iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :c, :b])
nil
Link to this function

each_key(map, func)

Specs

each_key(map(), function()) :: :ok

Calls the function with each key; returns :ok.

Examples

iex> RMap.each_key(%{a: 1, b: 2, c: 3}, &IO.inspect(&1))
# :a
# :b
# :c
:ok
Link to this function

each_pair(map, func)

See Enum.each/2.

Link to this function

each_value(map, func)

Specs

each_value(map(), function()) :: :ok

Calls the function with each value; returns :ok.

Examples

iex> RMap.each_value(%{a: 1, b: 2, c: 3}, &IO.inspect(&1))
# 1
# 2
# 3
:ok
Link to this function

eql?(map1, map2)

See Map.equal?/2.

Link to this function

except(map, keys)

Specs

except(map(), list()) :: map()

Returns a map excluding entries for the given keys.

Examples

iex> RMap.except(%{a: 1, b: 2, c: 3}, [:a, :b])
%{c: 3}
Link to this function

fetch_values(map, keys)

Specs

fetch_values(map(), list()) :: list()

Returns a list containing the values associated with the given keys.

Examples

iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "cat"])
["bovine", "feline"]

iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "bird"])
** (MapKeyError) key not found: bird
Link to this function

fetch_values(map, keys, func)

Specs

fetch_values(map(), list(), function()) :: list()

When a function is given, calls the function with each missing key, treating the block's return value as the value for that key.

Examples

iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "bird"], &(String.upcase(&1)))
["bovine", "BIRD"]
Link to this function

filter(map, func)

Specs

filter(map(), function()) :: map()

Returns a list whose entries are those for which the function returns a truthy value.

Examples

iex> RMap.filter(%{a: 1, b: 2, c: 3}, fn {_, v} -> v > 1 end)
%{b: 2, c: 3}

Specs

flatten(map()) :: list()

Returns a flatten list.

Examples

iex> RMap.flatten(%{1=> "one", 2 => [2,"two"], 3 => "three"})
[1, "one", 2, 2, "two", 3, "three"]

iex> RMap.flatten(%{1 => "one", 2 => %{a: 1, b: %{c: 3}}})
[1, "one", 2, :a, 1, :b, :c, 3]
Link to this function

has_value?(map, value)

See RMap.Ruby.value?/2.

See Kernel.inspect/1.

Specs

invert(map()) :: map()

Returns a map object with the each key-value pair inverted.

Examples

iex> RMap.invert(%{"a" => 0, "b" => 100, "c" => 200, "d" => 300, "e" => 300})
%{0 => "a", 100 => "b", 200 => "c", 300 => "e"}

iex> RMap.invert(%{a: 1, b: 1, c: %{d: 2}})
%{1 => :b, %{d: 2} => :c}
Link to this function

keep_if(map, func)

See RMap.Ruby.filter/2.

Link to this function

key(map, key, default \\ nil)

See Map.get/3.

See Map.has_key?/2.

See Enum.count/1.

Link to this function

rassoc(map, value)

Specs

rassoc(map(), any()) :: any()

Returns a 2-element tuple consisting of the key and value of the first-found entry having a given value.

Examples

iex> RMap.rassoc(%{a: 1, b: 2, c: 3}, 1)
{:a, 1}

iex> RMap.rassoc(%{a: 1, b: 2, c: 3}, 4)
nil

iex> RMap.rassoc(%{a: %{b: %{c: 1}}}, %{b: %{c: 1}})
{:a, %{b: %{c: 1}}}
Link to this function

reject(map, func)

Specs

reject(map(), function()) :: map()

Returns a list whose entries are all those from self for which the function returns false or nil.

Examples

iex> RMap.reject(%{a: 1, b: 2, c: 3}, fn {_, v} -> v > 1 end)
%{a: 1}
Link to this function

select(map, func)

See RMap.Ruby.filter/2.

Specs

shift(map()) :: {tuple() | nil, map()}

Removes the first map entry; returns a 2-element tuple. First element is {key, value}. Second element is a map without first pair.

Examples

iex> RMap.shift(%{a: 1, b: 2, c: 3})
{{:a, 1}, %{b: 2, c: 3}}

iex> RMap.shift(%{})
{nil, %{}}

See Enum.count/1.

Link to this function

store(map, key, value)

See Map.put/3.

Specs

to_hash(map()) :: map()

Returns given map.

Examples

iex> RMap.to_hash(%{a: 1, b: 2, c: 3})
%{a: 1, b: 2, c: 3}

See Kernel.inspect/1.

Link to this function

transform_keys(map, func)

Specs

transform_keys(map(), function()) :: map()

Returns a map with modified keys.

Examples

iex> RMap.transform_keys(%{a: 1, b: 2, c: 3}, &to_string(&1))
%{"a" => 1, "b" => 2, "c" => 3}

iex> RMap.transform_keys(%{a: %{b: %{c: 1}}}, &to_string(&1))
%{"a" => %{b: %{c: 1}}}
Link to this function

transform_values(map, func)

Specs

transform_values(map(), function()) :: map()

Returns a map with modified values.

Examples

iex> RMap.transform_values(%{a: 1, b: 2, c: 3}, &inspect(&1))
%{a: "1", b: "2", c: "3"}

iex> RMap.transform_values(%{a: %{b: %{c: 1}}}, &inspect(&1))
%{a: "%{b: %{c: 1}}"}
Link to this function

value?(map, value)

Specs

value?(map(), any()) :: boolean()

Returns true if value is a value in list, otherwise false.

Examples

iex> RMap.value?(%{a: 1, b: 2, c: 3}, 3)
true

iex> RMap.value?(%{a: 1, b: 2, c: 3}, 4)
false
Link to this function

values_at(map, keys)

Specs

values_at(map(), list()) :: list()

Returns a list containing values for the given keys.

Examples

iex> RMap.values_at(%{a: 1, b: 2, c: 3}, [:a, :b, :d])
[1, 2, nil]