class Hash(K, V)
- Hash(K, V)
- Reference
- Object
Overview
A Hash
represents a collection of key-value mappings, similar to a dictionary.
Main operations are storing a key-value mapping (#[]=
) and
querying the value associated to a key (#[]
). Key-value mappings can also be
deleted (#delete
).
Keys are unique within a hash. When adding a key-value mapping with a key that
is already in use, the old value will be forgotten.
# Create a new Hash for mapping String to Int32
hash = Hash(String, Int32).new
hash["one"] = 1
hash["two"] = 2
hash["one"] # => 1
Hash literals
can also be used to create a Hash
:
{"one" => 1, "two" => 2}
Implementation is based on an open hash table.
Two objects refer to the same hash key when their hash value (Object#hash
)
is identical and both objects are equal to each other (Object#==
).
Enumeration follows the order that the corresponding keys were inserted.
NOTE When using mutable data types as keys, changing the value of a key after
it was inserted into the Hash
may lead to undefined behaviour. This can be
restored by re-indexing the hash with #rehash
.
Included Modules
- Comparable(ReQL::AbstractValue)
- Enumerable({K, V})
- Iterable({K, V})
Defined in:
reql/executor/abstract_value.crInstance Method Summary
-
#<=>(other : ReQL::AbstractValue)
The comparison operator.
-
#==(other : ReQL::AbstractValue)
Compares this object to other based on the receiver’s
<=>
method, returningtrue
if it returns0
.
Instance methods inherited from class Object
!=(other : RethinkDB::DSL::R)
!=,
%(other : RethinkDB::DSL::R)
%,
&(other : RethinkDB::DSL::R)
&,
*(other : RethinkDB::DSL::R)
*,
+(other : RethinkDB::DSL::R)
+,
-(other : RethinkDB::DSL::R)
-,
/(other : RethinkDB::DSL::R)
/,
<(other : RethinkDB::DSL::R)
<,
<=(other : RethinkDB::DSL::R)
<=,
==(other : RethinkDB::DSL::R)
==,
>(other : RethinkDB::DSL::R)
>,
>=(other : RethinkDB::DSL::R)
>=,
|(other : RethinkDB::DSL::R)
|
Instance Method Detail
The comparison operator. Returns 0
if the two objects are equal,
a negative number if this object is considered less than other,
a positive number if this object is considered greater than other,
or nil
if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Compares this object to other based on the receiver’s <=>
method,
returning true
if it returns 0
.
Also returns true
if this and other are the same object.