abstract class AnyHash(K, V)
- AnyHash(K, V)
- Reference
- Object
Overview
Recursive wrapper for Hash(K, V).
Holds the given V values alone or recursively inside of a
container type like Array, Set or another Hash.
NOTE Performs deep cast of container types:
Array→Array(V)Tuple→Array(V)Set→Set(V)NamedTuple→Hash(K, V)Hash→Hash(K, V)AnyHash→Hash(K, V)
Included Modules
- Enumerable({K, V})
- Iterable({K, V})
Direct Known Subclasses
Defined in:
any_hash.crany_hash/json.cr
any_hash/version.cr
Constant Summary
-
CHAINED_HASH_METHODS =
["delete_if", "reject!", "select!", "compact!", "clear"] of ::String -
List of methods delegated to the underlying
Hashreturningself. -
VERSION =
"0.2.4"
Constructors
-
.new(hash = nil)
Initializes
AnyHashwith the given hash object.
Class Method Summary
-
.deep_cast_value(value)
Deep casts value to the
Vtype.
Instance Method Summary
-
#==(other : self)
ditto
-
#==(other : NamedTuple)
ditto
-
#==(arg)
See
Hash#==. -
#===(arg)
See
Hash#==. -
#[](*keys)
See
#dig. -
#[]=(key, value)
See
Hash#[]=. -
#[]=(*args)
See
Hash#[]=. -
#[]?(*keys)
See
#dig?. - #clone
-
#dig(keys : Enumerable)
Extracts the nested value specified by the sequence of keys objects by calling
#[]at each step, raises if any intermediate step isnil. -
#dig(*keys)
ditto
-
#dig?(keys : Enumerable)
Extracts the nested value specified by the sequence of keys objects by calling
#[]?at each step, returnsnilif any intermediate step isnil. -
#dig?(*keys)
ditto
- #dup
-
#each(*args, **options)
See
Hash#each. -
#each(*args, **options, &)
See
Hash#each. -
#merge(*values, **options)
Performs deep merge of
selfwith given other values and returns copy ofself. -
#merge(*values, **options, &)
ditto
-
#merge!(*values, **options)
Destructive version of
#merge. -
#merge!(*values, **options, &)
ditto
-
#replace(other)
Replaces underlying
Hashwith given other. -
#reverse_merge(other = nil, *values, **options)
Merges the caller into other.
-
#reverse_merge(other = nil, *values, **options, &)
ditto
-
#reverse_merge!(other = nil, *values, **options)
Destructive version of
#reverse_merge. -
#reverse_merge!(other = nil, *values, **options, &)
ditto
-
#to_h(*args, **options)
See
Hash#to_h. -
#to_h(*args, **options, &)
See
Hash#to_h.
Macro Summary
-
define_new(klass, key, value)
Defines new klass klass inheriting from
AnyHashwith given key keys and recursive value types. - method_missing(call)
Constructor Detail
Class Method Detail
Deep casts value to the V type.
ameba:disable Metrics/CyclomaticComplexity
Instance Method Detail
Returns a new AnyHash, with a deep copy of the underlying Hash.
Use #dup if you want a shallow copy.
Extracts the nested value specified by the sequence of keys objects
by calling #[] at each step, raises
if any intermediate step is nil.
Extracts the nested value specified by the sequence of keys objects
by calling #[]? at each step, returns nil
if any intermediate step is nil.
Returns a new AnyHash, with a shallow copy of the underlying Hash.
Use #clone if you want a deep copy.
Performs deep merge of self with given other values
and returns copy of self.
See Hash#merge.
Merges the caller into other. For example,
options = options.reverse_merge(size: 25, velocity: 10)
is equivalent to
options = { size: 25, velocity: 10 }.merge(options)
This is particularly useful for initializing an options hash with default values.
Macro Detail
Defines new klass klass inheriting from AnyHash
with given key keys and recursive value types.
AnyHash.define_new klass: :StringHash,
key: String,
value: String | Symbol
StringHash::Key # => String
StringHash::Value # => String | Symbol
StringHash.new({"foo" => {:bar, "baz"}})
# => #<StringHash:0x100d5e180 @__hash__={"foo" => [:bar, "baz"]}>
See AnyHash.