Class: Hash
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Enumerable
|
|
Inherits: | Object |
Defined in: | hash.c |
Class Method Summary
-
.[] ⇒ Hash
Returns a new Hash object populated with the given objects, if any.
-
.new(default_value = nil) ⇒ Hash
constructor
Returns a new empty Hash object.
-
.ruby2_keywords_hash(hash) ⇒ Hash
Duplicates a given hash and adds a ruby2_keywords flag.
-
.ruby2_keywords_hash?(hash) ⇒ Boolean
Checks if a given hash is flagged by Module#ruby2_keywords (or Proc#ruby2_keywords).
-
.try_convert(obj) ⇒ Object, ...
If
obj
is a Hash object, returnsobj
.
Instance Attribute Summary
-
#compare_by_identity ⇒ self
readonly
Sets
self
to consider only identity in comparing keys; two keys are considered the same only if they are the same object; returnsself
. -
#compare_by_identity? ⇒ Boolean
readonly
Returns
true
if #compare_by_identity has been called,false
otherwise. -
#default_proc ⇒ Proc?
rw
Returns the default proc for
self
(seeDefault Values
): -
#default_proc=(proc) ⇒ Proc
rw
Sets the default proc for
self
toproc
: (seeDefault Values
): -
#empty? ⇒ Boolean
readonly
Returns
true
if there are no hash entries,false
otherwise:
Instance Method Summary
-
#<(other_hash) ⇒ Boolean
Returns
true
if #hash is a proper subset ofother_hash
,false
otherwise: -
#<=(other_hash) ⇒ Boolean
Returns
true
if #hash is a subset ofother_hash
,false
otherwise: -
#==(object) ⇒ Boolean
Returns
true
if all of the following are true: *object
is a Hash object. -
#>(other_hash) ⇒ Boolean
Returns
true
if #hash is a proper superset ofother_hash
,false
otherwise: -
#>=(other_hash) ⇒ Boolean
Returns
true
if #hash is a superset ofother_hash
,false
otherwise: -
#[](key) ⇒ value
Returns the value associated with the given #key, if found:
-
#[]=(key, value) ⇒ value
(also: #store)
Associates the given
value
with the given #key; returnsvalue
. -
#any? ⇒ Boolean
Returns
true
if any element satisfies a given criterion;false
otherwise. - #assoc(key) ⇒ Array?
-
#clear ⇒ self
Removes all hash entries; returns
self
. -
#compact ⇒ Hash
Returns a copy of
self
with allnil
-valued entries removed: -
#compact! ⇒ self?
Returns
self
with all itsnil
-valued entries removed (in place): -
#default ⇒ Object
Returns the default value for the given #key.
-
#default=(value) ⇒ Object
Sets the default value to
value
; returnsvalue
: -
#delete(key) ⇒ value?
Deletes the entry for the given #key and returns its associated value.
-
#delete_if {|key, value| ... } ⇒ self
If a block given, calls the block with each key-value pair; deletes each entry for which the block returns a truthy value; returns
self
: -
#dig(key, *identifiers) ⇒ Object
Finds and returns the object in nested objects that is specified by #key and
identifiers
. -
#each {|key, value| ... } ⇒ self
(also: #each_pair)
Calls the given block with each key-value pair; returns
self
: -
#each_key {|key| ... } ⇒ self
Calls the given block with each key; returns
self
: -
#each_pair {|key, value| ... } ⇒ self
Alias for #each.
-
#each_value {|value| ... } ⇒ self
Calls the given block with each value; returns
self
: -
#eql?(object) ⇒ Boolean
Returns
true
if all of the following are true: *object
is a Hash object. -
#except(*keys) ⇒ Hash
Returns a new Hash excluding entries for the given #keys:
-
#fetch(key) ⇒ Object
Returns the value for the given #key, if found.
-
#fetch_values(*keys) ⇒ Array
Returns a new
::Array
containing the values associated with the given keys *keys: -
#select {|key, value| ... } ⇒ Hash
(also: #select)
Returns a new Hash object whose entries are those for which the block returns a truthy value:
-
#select! {|key, value| ... } ⇒ self?
(also: #select!)
Returns
self
, whose entries are those for which the block returns a truthy value: -
#flatten ⇒ Array
Returns a new
::Array
object that is a 1-dimensional flattening ofself
. -
#has_key?(key) ⇒ Boolean
Alias for #key?.
-
#has_value?(value) ⇒ Boolean
Alias for #value?.
-
#hash ⇒ Integer
Returns the
::Integer
hash-code for the hash. -
#include?(key) ⇒ Boolean
Alias for #key?.
-
#initialize_copy(other_hash) ⇒ self
Alias for #replace.
-
#inspect ⇒ String
Alias for #to_s.
-
#invert ⇒ Hash
Returns a new Hash object with the each key-value pair inverted:
-
#keep_if {|key, value| ... } ⇒ self
Calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry; returns
self
. -
#key(value) ⇒ key?
Returns the key for the first-found entry with the given
value
(seeEntry Order
): -
#key?(key) ⇒ Boolean
(also: #include?, #member?, #has_key?)
Returns
true
if #key is a key inself
, otherwisefalse
. -
#keys ⇒ Array
Returns a new
::Array
containing all keys inself
: -
#length ⇒ Integer
(also: #size)
Returns the count of entries in
self
: -
#member?(key) ⇒ Boolean
Alias for #key?.
-
#merge ⇒ copy_of_self
Returns the new Hash formed by merging each of
other_hashes
into a copy ofself
. -
#merge! ⇒ self
(also: #update)
Merges each of
other_hashes
intoself
; returnsself
. - #rassoc(value) ⇒ Array?
-
#rehash ⇒ self
Rebuilds the hash table by recomputing the hash index for each key; returns
self
. -
#reject {|key, value| ... } ⇒ Hash
Returns a new Hash object whose entries are all those from
self
for which the block returnsfalse
ornil
: -
#reject! {|key, value| ... } ⇒ self?
Returns
self
, whose remaining entries are those for which the block returnsfalse
ornil
: -
#replace(other_hash) ⇒ self
(also: #initialize_copy)
Replaces the entire contents of
self
with the contents ofother_hash
; returnsself
: -
#select {|key, value| ... } ⇒ Hash
Alias for #filter.
-
#select! {|key, value| ... } ⇒ self?
Alias for #filter!.
-
#shift ⇒ Array, value
Removes the first hash entry (see
Entry Order
); returns a 2-element::Array
containing the removed key and value: -
#size ⇒ Integer
Alias for #length.
-
#slice(*keys) ⇒ Hash
Returns a new Hash object containing the entries for the given #keys:
-
#store(key, value)
Alias for #[]=.
- #to_a ⇒ Array
-
#to_h ⇒ self, Hash
For an instance of Hash, returns
self
. -
#to_hash ⇒ self
Returns
self
. -
#to_proc ⇒ Proc
Returns a
::Proc
object that maps a key to its value: -
#to_s ⇒ String
(also: #inspect)
Returns a new
::String
containing the hash entries: -
#transform_keys {|key| ... } ⇒ Hash
Returns a new Hash object; each entry has: * A key provided by the block.
-
#transform_keys! {|key| ... } ⇒ self
Same as #transform_keys but modifies the receiver in place instead of returning a new hash.
-
#transform_values {|value| ... } ⇒ Hash
Returns a new Hash object; each entry has: * A key from
self
. -
#transform_values! {|value| ... } ⇒ self
Returns
self
, whose keys are unchanged, and whose values are determined by the given block. -
#merge! ⇒ self
Alias for #merge!.
-
#value?(value) ⇒ Boolean
(also: #has_value?)
Returns
true
ifvalue
is a value inself
, otherwisefalse
. -
#values ⇒ Array
Returns a new
::Array
containing all values inself
: - #values_at(*keys) ⇒ Array
- #deconstruct_keys(keys) Internal use only
::Enumerable
- Included
#all? | Returns whether every element meets a given criterion. |
#any? | Returns whether any element meets a given criterion. |
#chain | Returns an enumerator object generated from this enumerator and given enumerables. |
#chunk | Each element in the returned enumerator is a 2-element array consisting of: |
#chunk_while | Creates an enumerator for each chunked elements. |
#collect | Alias for Enumerable#map. |
#collect_concat | Alias for Enumerable#flat_map. |
#compact | Returns an array of all non- |
#count | Returns the count of elements, based on an argument or block criterion, if given. |
#cycle | When called with positive integer argument |
#detect | Alias for Enumerable#find. |
#drop | For positive integer |
#drop_while | Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements after that point: |
#each_cons | Calls the block with each successive overlapped |
#each_entry | Calls the given block with each element, converting multiple values from yield to an array; returns |
#each_slice | Calls the block with each successive disjoint |
#each_with_index | With a block given, calls the block with each element and its index; returns |
#each_with_object | Calls the block once for each element, passing both the element and the given object: |
#entries | Alias for Enumerable#to_a. |
#filter | Returns an array containing elements selected by the block. |
#filter_map | Returns an array containing truthy elements returned by the block. |
#find | Returns the first element for which the block returns a truthy value. |
#find_all | Alias for Enumerable#filter. |
#find_index | Returns the index of the first element that meets a specified criterion, or |
#first | Returns the first element or elements. |
#flat_map | Returns an array of flattened objects returned by the block. |
#grep | Returns an array of objects based elements of |
#grep_v | Returns an array of objects based on elements of |
#group_by | With a block given returns a hash: |
#include? | Alias for Enumerable#member?. |
#inject | Returns an object formed from operands via either: |
#lazy | Returns an |
#map | Returns an array of objects returned by the block. |
#max | Returns the element with the maximum element according to a given criterion. |
#max_by | Returns the elements for which the block returns the maximum values. |
#member? | Returns whether for any element |
#min | Returns the element with the minimum element according to a given criterion. |
#min_by | Returns the elements for which the block returns the minimum values. |
#minmax | Returns a 2-element array containing the minimum and maximum elements according to a given criterion. |
#minmax_by | Returns a 2-element array containing the elements for which the block returns minimum and maximum values: |
#none? | Returns whether no element meets a given criterion. |
#one? | Returns whether exactly one element meets a given criterion. |
#partition | With a block given, returns an array of two arrays: |
#reduce | Alias for Enumerable#inject. |
#reject | Returns an array of objects rejected by the block. |
#reverse_each | With a block given, calls the block with each element, but in reverse order; returns |
#select | Alias for Enumerable#filter. |
#slice_after | Creates an enumerator for each chunked elements. |
#slice_before | With argument |
#slice_when | Creates an enumerator for each chunked elements. |
#sort | Returns an array containing the sorted elements of |
#sort_by | With a block given, returns an array of elements of |
#sum | With no block given, returns the sum of |
#take | For non-negative integer |
#take_while | Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements up to that point: |
#tally | Returns a hash containing the counts of equal elements: |
#to_a | Returns an array containing the items in |
#to_h | When |
#to_set | Makes a set from the enumerable object with given arguments. |
#uniq | With no block, returns a new array containing only unique elements; the array has no two elements |
#zip | With no block given, returns a new array |
Constructor Details
.new(default_value = nil) ⇒ Hash
.new {|hash, key| ... } ⇒ Hash
Hash
.new {|hash, key| ... } ⇒ Hash
Returns a new empty Hash object.
The initial default value and initial default proc for the new hash depend on which form above was used. See Default Values
.
If neither an argument nor a block given, initializes both the default value and the default proc to nil
:
h = Hash.new
h.default # => nil
h.default_proc # => nil
If argument default_value
given but no block given, initializes the default value to the given default_value
and the default proc to nil
:
h = Hash.new(false)
h.default # => false
h.default_proc # => nil
If a block given but no argument, stores the block as the default proc and sets the default value to nil
:
h = Hash.new {|hash, key| "Default value for #{key}" }
h.default # => nil
h.default_proc.class # => Proc
h[:nosuch] # => "Default value for nosuch"
# File 'hash.c', line 1779
static VALUE rb_hash_initialize(int argc, VALUE *argv, VALUE hash) { rb_hash_modify(hash); if (rb_block_given_p()) { rb_check_arity(argc, 0, 0); SET_PROC_DEFAULT(hash, rb_block_proc()); } else { rb_check_arity(argc, 0, 1); VALUE options, ifnone; rb_scan_args(argc, argv, "01:", &ifnone, &options); if (NIL_P(ifnone) && !NIL_P(options)) { ifnone = options; rb_warn_deprecated_to_remove("3.4", "Calling Hash.new with keyword arguments", "Hash.new({ key: value })"); } RHASH_SET_IFNONE(hash, ifnone); } return hash; }
Class Method Details
.[] ⇒ Hash
.[](hash) ⇒ Hash
.[]([*2_element_arrays] ) ⇒ Hash
.[](*objects) ⇒ Hash
Hash
.[](hash) ⇒ Hash
.[]([*2_element_arrays] ) ⇒ Hash
.[](*objects) ⇒ Hash
Returns a new Hash object populated with the given objects, if any. See .new.
With no argument, returns a new empty Hash.
When the single given argument is a Hash, returns a new Hash populated with the entries from the given Hash, excluding the default value or proc.
h = {foo: 0, bar: 1, baz: 2}
Hash[h] # => {:foo=>0, :bar=>1, :baz=>2}
When the single given argument is an ::Array
of 2-element Arrays, returns a new Hash object wherein each 2-element array forms a key-value entry:
Hash[ [ [:foo, 0], [:, 1] ] ] # => {:foo=>0, :bar=>1}
When the argument count is an even number; returns a new Hash object wherein each successive pair of arguments has become a key-value entry:
Hash[:foo, 0, :, 1] # => {:foo=>0, :bar=>1}
Raises an exception if the argument list does not conform to any of the above.
# File 'hash.c', line 1840
static VALUE rb_hash_s_create(int argc, VALUE *argv, VALUE klass) { VALUE hash, tmp; if (argc == 1) { tmp = rb_hash_s_try_convert(Qnil, argv[0]); if (!NIL_P(tmp)) { if (!RHASH_EMPTY_P(tmp) && rb_hash_compare_by_id_p(tmp)) { /* hash_copy for non-empty hash will copy compare_by_identity flag, but we don't want it copied. Work around by converting hash to flattened array and using that. */ tmp = rb_hash_to_a(tmp); } else { hash = hash_alloc(klass); if (!RHASH_EMPTY_P(tmp)) hash_copy(hash, tmp); return hash; } } else { tmp = rb_check_array_type(argv[0]); } if (!NIL_P(tmp)) { long i; hash = hash_alloc(klass); for (i = 0; i < RARRAY_LEN(tmp); ++i) { VALUE e = RARRAY_AREF(tmp, i); VALUE v = rb_check_array_type(e); VALUE key, val = Qnil; if (NIL_P(v)) { rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)", rb_builtin_class_name(e), i); } switch (RARRAY_LEN(v)) { default: rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)", RARRAY_LEN(v)); case 2: val = RARRAY_AREF(v, 1); case 1: key = RARRAY_AREF(v, 0); rb_hash_aset(hash, key, val); } } return hash; } } if (argc % 2 != 0) { rb_raise(rb_eArgError, "odd number of arguments for Hash"); } hash = hash_alloc(klass); rb_hash_bulk_insert(argc, argv, hash); hash_verify(hash); return hash; }
.ruby2_keywords_hash(hash) ⇒ Hash
Duplicates a given hash and adds a ruby2_keywords flag. This method is not for casual use; debugging, researching, and some truly necessary cases like deserialization of arguments.
h = {k: 1}
h = Hash.ruby2_keywords_hash(h)
def foo(k: 42)
k
end
foo(*[h]) #=> 1 with neither a warning or an error
# File 'hash.c', line 1971
static VALUE rb_hash_s_ruby2_keywords_hash(VALUE dummy, VALUE hash) { Check_Type(hash, T_HASH); VALUE tmp = rb_hash_dup(hash); if (RHASH_EMPTY_P(hash) && rb_hash_compare_by_id_p(hash)) { rb_hash_compare_by_id(tmp); } RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS; return tmp; }
.ruby2_keywords_hash?(hash) ⇒ Boolean
Checks if a given hash is flagged by Module#ruby2_keywords (or Proc#ruby2_keywords). This method is not for casual use; debugging, researching, and some truly necessary cases like serialization of arguments.
ruby2_keywords def foo(*args)
Hash.ruby2_keywords_hash?(args.last)
end
foo(k: 1) #=> true
foo({k: 1}) #=> false
# File 'hash.c', line 1949
static VALUE rb_hash_s_ruby2_keywords_hash_p(VALUE dummy, VALUE hash) { Check_Type(hash, T_HASH); return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS); }
.try_convert(obj) ⇒ Object, ...
If obj
is a Hash object, returns obj
.
Otherwise if obj
responds to :to_hash
, calls obj.to_hash
and returns the result.
Returns nil
if obj
does not respond to :to_hash
Raises an exception unless obj.to_hash
returns a Hash object.
# File 'hash.c', line 1928
static VALUE rb_hash_s_try_convert(VALUE dummy, VALUE hash) { return rb_check_hash_type(hash); }
Instance Attribute Details
#compare_by_identity ⇒ self
(readonly)
Sets self
to consider only identity in comparing keys; two keys are considered the same only if they are the same object; returns self
.
By default, these two object are considered to be the same key, so s1
will overwrite s0
:
s0 = 'x'
s1 = 'x'
h = {}
h.compare_by_identity? # => false
h[s0] = 0
h[s1] = 1
h # => {"x"=>1}
After calling #compare_by_identity, the keys are considered to be different, and therefore do not overwrite each other:
h = {}
h.compare_by_identity # => {}
h.compare_by_identity? # => true
h[s0] = 0
h[s1] = 1
h # => {"x"=>0, "x"=>1}
# File 'hash.c', line 4373
VALUE rb_hash_compare_by_id(VALUE hash) { VALUE tmp; st_table *identtable; if (rb_hash_compare_by_id_p(hash)) return hash; rb_hash_modify_check(hash); if (hash_iterating_p(hash)) { rb_raise(rb_eRuntimeError, "compare_by_identity during iteration"); } if (RHASH_TABLE_EMPTY_P(hash)) { // Fast path: There's nothing to rehash, so we don't need a `tmp` table. // We're most likely an AR table, so this will need an allocation. ar_force_convert_table(hash, __FILE__, __LINE__); HASH_ASSERT(RHASH_ST_TABLE_P(hash)); RHASH_ST_TABLE(hash)->type = &identhash; } else { // Slow path: Need to rehash the members of `self` into a new // `tmp` table using the new `identhash` compare/hash functions. tmp = hash_alloc(0); hash_st_table_init(tmp, &identhash, RHASH_SIZE(hash)); identtable = RHASH_ST_TABLE(tmp); rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); rb_hash_free(hash); // We know for sure `identtable` is an st table, // so we can skip `ar_force_convert_table` here. RHASH_ST_TABLE_SET(hash, identtable); RHASH_ST_CLEAR(tmp); } return hash; }
#compare_by_identity? ⇒ Boolean
(readonly)
Returns true
if #compare_by_identity has been called, false
otherwise.
# File 'hash.c', line 4420
VALUE rb_hash_compare_by_id_p(VALUE hash) { return RBOOL(RHASH_IDENTHASH_P(hash)); }
#default_proc ⇒ Proc? (rw)
# File 'hash.c', line 2282
static VALUE rb_hash_default_proc(VALUE hash) { if (FL_TEST(hash, RHASH_PROC_DEFAULT)) { return RHASH_IFNONE(hash); } return Qnil; }
#default_proc=(proc) ⇒ Proc (rw)
Sets the default proc for self
to proc
: (see Default Values
):
h = {}
h.default_proc # => nil
h.default_proc = proc { |hash, key| "Default value for #{key}" }
h.default_proc.class # => Proc
h.default_proc = nil
h.default_proc # => nil
# File 'hash.c', line 2305
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc) { VALUE b; rb_hash_modify_check(hash); if (NIL_P(proc)) { SET_DEFAULT(hash, proc); return proc; } b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc); if (NIL_P(b) || !rb_obj_is_proc(b)) { rb_raise(rb_eTypeError, "wrong default_proc type %s (expected Proc)", rb_obj_classname(proc)); } proc = b; SET_PROC_DEFAULT(hash, proc); return proc; }
#empty? ⇒ Boolean
(readonly)
Returns true
if there are no hash entries, false
otherwise:
{}.empty? # => true
{foo: 0, bar: 1, baz: 2}.empty? # => false
# File 'hash.c', line 3020
static VALUE rb_hash_empty_p(VALUE hash) { return RBOOL(RHASH_EMPTY_P(hash)); }
Instance Method Details
#<(other_hash) ⇒ Boolean
Returns true
if #hash is a proper subset of other_hash
, false
otherwise:
h1 = {foo: 0, bar: 1}
h2 = {foo: 0, bar: 1, baz: 2}
h1 < h2 # => true
h2 < h1 # => false
h1 < h1 # => false
# File 'hash.c', line 4633
static VALUE rb_hash_lt(VALUE hash, VALUE other) { other = to_hash(other); if (RHASH_SIZE(hash) >= RHASH_SIZE(other)) return Qfalse; return hash_le(hash, other); }
#<=(other_hash) ⇒ Boolean
Returns true
if #hash is a subset of other_hash
, false
otherwise:
h1 = {foo: 0, bar: 1}
h2 = {foo: 0, bar: 1, baz: 2}
h1 <= h2 # => true
h2 <= h1 # => false
h1 <= h1 # => true
# File 'hash.c', line 4614
static VALUE rb_hash_le(VALUE hash, VALUE other) { other = to_hash(other); if (RHASH_SIZE(hash) > RHASH_SIZE(other)) return Qfalse; return hash_le(hash, other); }
#==(object) ⇒ Boolean
Returns true
if all of the following are true:
-
object
is a Hash object. -
#hash and
object
have the same keys (regardless of order). -
For each key #key,
hash[key] == object[key]
.
Otherwise, returns false
.
Equal:
h1 = {foo: 0, bar: 1, baz: 2}
h2 = {foo: 0, bar: 1, baz: 2}
h1 == h2 # => true
h3 = {baz: 2, bar: 1, foo: 0}
h1 == h3 # => true
# File 'hash.c', line 3791
static VALUE rb_hash_equal(VALUE hash1, VALUE hash2) { return hash_equal(hash1, hash2, FALSE); }
#>(other_hash) ⇒ Boolean
Returns true
if #hash is a proper superset of other_hash
, false
otherwise:
h1 = {foo: 0, bar: 1, baz: 2}
h2 = {foo: 0, bar: 1}
h1 > h2 # => true
h2 > h1 # => false
h1 > h1 # => false
# File 'hash.c', line 4671
static VALUE rb_hash_gt(VALUE hash, VALUE other) { other = to_hash(other); if (RHASH_SIZE(hash) <= RHASH_SIZE(other)) return Qfalse; return hash_le(other, hash); }
#>=(other_hash) ⇒ Boolean
Returns true
if #hash is a superset of other_hash
, false
otherwise:
h1 = {foo: 0, bar: 1, baz: 2}
h2 = {foo: 0, bar: 1}
h1 >= h2 # => true
h2 >= h1 # => false
h1 >= h1 # => true
# File 'hash.c', line 4652
static VALUE rb_hash_ge(VALUE hash, VALUE other) { other = to_hash(other); if (RHASH_SIZE(hash) < RHASH_SIZE(other)) return Qfalse; return hash_le(other, hash); }
#[](key) ⇒ value
# File 'hash.c', line 2118
VALUE rb_hash_aref(VALUE hash, VALUE key) { st_data_t val; if (hash_stlike_lookup(hash, key, &val)) { return (VALUE)val; } else { return rb_hash_default_value(hash, key); } }
#[]=(key, value) ⇒ value
#store(key, value)
Also known as: #store
value
#store(key, value)
Associates the given value
with the given #key; returns value
.
If the given #key exists, replaces its value with the given value
; the ordering is not affected (see Entry Order
):
h = {foo: 0, bar: 1}
h[:foo] = 2 # => 2
h.store(:, 3) # => 3
h # => {:foo=>2, :bar=>3}
If #key does not exist, adds the #key and value
; the new entry is last in the order (see Entry Order
):
h = {foo: 0, bar: 1}
h[:baz] = 2 # => 2
h.store(:bat, 3) # => 3
h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
# File 'hash.c', line 2938
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val) { bool iter_p = hash_iterating_p(hash); rb_hash_modify(hash); if (!RHASH_STRING_KEY_P(hash, key)) { RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val); } else { RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val); } return val; }
#any? ⇒ Boolean
#any?(object) ⇒ Boolean
#any? {|key, value| ... } ⇒ Boolean
Boolean
#any?(object) ⇒ Boolean
#any? {|key, value| ... } ⇒ Boolean
Returns true
if any element satisfies a given criterion; false
otherwise.
If self
has no element, returns false
and argument or block are not used.
With no argument and no block, returns true
if self
is non-empty; false
if empty.
With argument object
and no block, returns true
if for any key #key h.assoc(key) == object
:
h = {foo: 0, bar: 1, baz: 2}
h.any?([:, 1]) # => true
h.any?([:, 0]) # => false
h.any?([:baz, 1]) # => false
With no argument and a block, calls the block with each key-value pair; returns true
if the block returns any truthy value, false
otherwise:
h = {foo: 0, bar: 1, baz: 2}
h.any? {|key, value| value < 3 } # => true
h.any? {|key, value| value > 3 } # => false
Related: Enumerable#any?
# File 'hash.c', line 4515
static VALUE rb_hash_any_p(int argc, VALUE *argv, VALUE hash) { VALUE args[2]; args[0] = Qfalse; rb_check_arity(argc, 0, 1); if (RHASH_EMPTY_P(hash)) return Qfalse; if (argc) { if (rb_block_given_p()) { rb_warn("given block not used"); } args[1] = argv[0]; rb_hash_foreach(hash, any_p_i_pattern, (VALUE)args); } else { if (!rb_block_given_p()) { /* yields pairs, never false */ return Qtrue; } if (rb_block_pair_yield_optimizable()) rb_hash_foreach(hash, any_p_i_fast, (VALUE)args); else rb_hash_foreach(hash, any_p_i, (VALUE)args); } return args[0]; }
#assoc(key) ⇒ Array?
# File 'hash.c', line 4139
static VALUE rb_hash_assoc(VALUE hash, VALUE key) { VALUE args[2]; if (RHASH_EMPTY_P(hash)) return Qnil; if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) { VALUE value = Qundef; st_table assoctable = *RHASH_ST_TABLE(hash); assoctable.type = &(struct st_hash_type){ .compare = assoc_cmp, .hash = assoctable.type->hash, }; VALUE arg = (VALUE)&(struct assoc_arg){ .tbl = &assoctable, .key = (st_data_t)key, }; if (RB_OBJ_FROZEN(hash)) { value = assoc_lookup(arg); } else { hash_iter_lev_inc(hash); value = rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash); } hash_verify(hash); if (!UNDEF_P(value)) return rb_assoc_new(key, value); } args[0] = key; args[1] = Qnil; rb_hash_foreach(hash, assoc_i, (VALUE)args); return args[1]; }
#clear ⇒ self
Removes all hash entries; returns self
.
# File 'hash.c', line 2865
VALUE rb_hash_clear(VALUE hash) { rb_hash_modify_check(hash); if (hash_iterating_p(hash)) { rb_hash_foreach(hash, clear_i, 0); } else if (RHASH_AR_TABLE_P(hash)) { ar_clear(hash); } else { st_clear(RHASH_ST_TABLE(hash)); compact_after_delete(hash); } return hash; }
#compact ⇒ Hash
Returns a copy of self
with all nil
-valued entries removed:
h = {foo: 0, bar: nil, baz: 2, bat: nil}
h1 = h.compact
h1 # => {:foo=>0, :baz=>2}
# File 'hash.c', line 4306
static VALUE rb_hash_compact(VALUE hash) { VALUE result = rb_hash_dup(hash); if (!RHASH_EMPTY_P(hash)) { rb_hash_foreach(result, delete_if_nil, result); compact_after_delete(result); } else if (rb_hash_compare_by_id_p(hash)) { result = rb_hash_compare_by_id(result); } return result; }
#compact! ⇒ self
?
Returns self
with all its nil
-valued entries removed (in place):
h = {foo: 0, bar: nil, baz: 2, bat: nil}
h.compact! # => {:foo=>0, :baz=>2}
Returns nil
if no entries were removed.
# File 'hash.c', line 4331
static VALUE rb_hash_compact_bang(VALUE hash) { st_index_t n; rb_hash_modify_check(hash); n = RHASH_SIZE(hash); if (n) { rb_hash_foreach(hash, delete_if_nil, hash); if (n != RHASH_SIZE(hash)) return hash; } return Qnil; }
#deconstruct_keys(keys)
# File 'hash.c', line 4705
static VALUE rb_hash_deconstruct_keys(VALUE hash, VALUE keys) { return hash; }
Returns the default value for the given #key. The returned value will be determined either by the default proc or by the default value. See Default Values
.
With no argument, returns the current default value:
h = {}
h.default # => nil
If #key is given, returns the default value for #key, regardless of whether that key exists:
h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
h[:foo] = "Hello"
h.default(:foo) # => "No key foo"
# File 'hash.c', line 2235
static VALUE rb_hash_default(int argc, VALUE *argv, VALUE hash) { VALUE ifnone; rb_check_arity(argc, 0, 1); ifnone = RHASH_IFNONE(hash); if (FL_TEST(hash, RHASH_PROC_DEFAULT)) { if (argc == 0) return Qnil; return call_default_proc(ifnone, hash, argv[0]); } return ifnone; }
#default=(value) ⇒ Object
# File 'hash.c', line 2262
static VALUE rb_hash_set_default(VALUE hash, VALUE ifnone) { rb_hash_modify_check(hash); SET_DEFAULT(hash, ifnone); return ifnone; }
#delete(key) ⇒ value
?
#delete(key) {|key| ... } ⇒ Object
value
?
#delete(key) {|key| ... } ⇒ Object
Deletes the entry for the given #key and returns its associated value.
If no block is given and #key is found, deletes the entry and returns the associated value:
h = {foo: 0, bar: 1, baz: 2}
h.delete(: ) # => 1
h # => {:foo=>0, :baz=>2}
If no block given and #key is not found, returns nil
.
If a block is given and #key is found, ignores the block, deletes the entry, and returns the associated value:
h = {foo: 0, bar: 1, baz: 2}
h.delete(:baz) { |key| raise 'Will never happen'} # => 2
h # => {:foo=>0, :bar=>1}
If a block is given and #key is not found, calls the block and returns the block’s return value:
h = {foo: 0, bar: 1, baz: 2}
h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
h # => {:foo=>0, :bar=>1, :baz=>2}
# File 'hash.c', line 2438
static VALUE rb_hash_delete_m(VALUE hash, VALUE key) { VALUE val; rb_hash_modify_check(hash); val = rb_hash_delete_entry(hash, key); if (!UNDEF_P(val)) { compact_after_delete(hash); return val; } else { if (rb_block_given_p()) { return rb_yield(key); } else { return Qnil; } } }
#delete_if {|key, value| ... } ⇒ self
#delete_if ⇒ Enumerator
self
#delete_if ⇒ Enumerator
If a block given, calls the block with each key-value pair; deletes each entry for which the block returns a truthy value; returns self
:
h = {foo: 0, bar: 1, baz: 2}
h.delete_if {|key, value| value > 0 } # => {:foo=>0}
If no block given, returns a new ::Enumerator
:
h = {foo: 0, bar: 1, baz: 2}
e = h.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
e.each { |key, value| value > 0 } # => {:foo=>0}
# File 'hash.c', line 2561
VALUE rb_hash_delete_if(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify_check(hash); if (!RHASH_TABLE_EMPTY_P(hash)) { rb_hash_foreach(hash, delete_if_i, hash); compact_after_delete(hash); } return hash; }
#dig(key, *identifiers) ⇒ Object
Finds and returns the object in nested objects that is specified by #key and identifiers
. The nested objects may be instances of various classes. See Dig Methods
.
Nested Hashes:
h = {foo: {bar: {baz: 2}}}
h.dig(:foo) # => {:bar=>{:baz=>2}}
h.dig(:foo, : ) # => {:baz=>2}
h.dig(:foo, :, :baz) # => 2
h.dig(:foo, :, :BAZ) # => nil
Nested Hashes and Arrays:
h = {foo: {bar: [:a, :b, :c]}}
h.dig(:foo, :, 2) # => :c
This method will use the default values
for keys that are not present:
h = {foo: {bar: [:a, :b, :c]}}
h.dig(:hello) # => nil
h.default_proc = -> (hash, _key) { hash }
h.dig(:hello, :world) # => h
h.dig(:hello, :world, :foo, :, 2) # => :c
# File 'hash.c', line 4573
static VALUE rb_hash_dig(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); self = rb_hash_aref(self, *argv); if (!--argc) return self; ++argv; return rb_obj_dig(argc, argv, self, Qnil); }
#each {|key, value| ... } ⇒ self
#each_pair {|key, value| ... } ⇒ self
#each ⇒ Enumerator
#each_pair ⇒ Enumerator
Also known as: #each_pair
self
#each_pair {|key, value| ... } ⇒ self
#each ⇒ Enumerator
#each_pair ⇒ Enumerator
Calls the given block with each key-value pair; returns self
:
h = {foo: 0, bar: 1, baz: 2}
h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo: 0
: 1
baz: 2
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
h1 = e.each {|key, value| puts "#{key}: #{value}"}
h1 # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo: 0
: 1
baz: 2
# File 'hash.c', line 3146
static VALUE rb_hash_each_pair(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); if (rb_block_pair_yield_optimizable()) rb_hash_foreach(hash, each_pair_i_fast, 0); else rb_hash_foreach(hash, each_pair_i, 0); return hash; }
#each_key {|key| ... } ⇒ self
#each_key ⇒ Enumerator
self
#each_key ⇒ Enumerator
Calls the given block with each key; returns self
:
h = {foo: 0, bar: 1, baz: 2}
h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo
baz
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>
h1 = e.each {|key| puts key }
h1 # => {:foo=>0, :bar=>1, :baz=>2}
Output:
foo
baz
# File 'hash.c', line 3095
static VALUE rb_hash_each_key(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_foreach(hash, each_key_i, 0); return hash; }
#each {|key, value| ... } ⇒ self
#each_pair {|key, value| ... } ⇒ self
#each ⇒ Enumerator
#each_pair ⇒ Enumerator
self
#each_pair {|key, value| ... } ⇒ self
#each ⇒ Enumerator
#each_pair ⇒ Enumerator
Alias for #each.
#each_value {|value| ... } ⇒ self
#each_value ⇒ Enumerator
self
#each_value ⇒ Enumerator
Calls the given block with each value; returns self
:
h = {foo: 0, bar: 1, baz: 2}
h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
Output:
0
1
2
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>
h1 = e.each {|value| puts value }
h1 # => {:foo=>0, :bar=>1, :baz=>2}
Output:
0
1
2
# File 'hash.c', line 3057
static VALUE rb_hash_each_value(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_foreach(hash, each_value_i, 0); return hash; }
#eql?(object) ⇒ Boolean
Returns true
if all of the following are true:
-
object
is a Hash object. -
#hash and
object
have the same keys (regardless of order). -
For each key #key,
h[key] eql? object[key]
.
Otherwise, returns false
.
Equal:
h1 = {foo: 0, bar: 1, baz: 2}
h2 = {foo: 0, bar: 1, baz: 2}
h1.eql? h2 # => true
h3 = {baz: 2, bar: 1, foo: 0}
h1.eql? h3 # => true
# File 'hash.c', line 3816
static VALUE rb_hash_eql(VALUE hash1, VALUE hash2) { return hash_equal(hash1, hash2, TRUE); }
#except(*keys) ⇒ Hash
# File 'hash.c', line 2680
static VALUE rb_hash_except(int argc, VALUE *argv, VALUE hash) { int i; VALUE key, result; result = hash_dup_with_compare_by_id(hash); for (i = 0; i < argc; i++) { key = argv[i]; rb_hash_delete(result, key); } compact_after_delete(result); return result; }
Returns the value for the given #key, if found.
h = {foo: 0, bar: 1, baz: 2}
h.fetch(: ) # => 1
If #key is not found and no block was given, returns default_value
:
{}.fetch(:nosuch, :default) # => :default
If #key is not found and a block was given, yields #key to the block and returns the block’s return value:
{}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
Raises KeyError if neither default_value
nor a block was given.
Note that this method does not use the values of either #default or #default_proc.
# File 'hash.c', line 2173
static VALUE rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash) { VALUE key; st_data_t val; long block_given; rb_check_arity(argc, 1, 2); key = argv[0]; block_given = rb_block_given_p(); if (block_given && argc == 2) { rb_warn("block supersedes default value argument"); } if (hash_stlike_lookup(hash, key, &val)) { return (VALUE)val; } else { if (block_given) { return rb_yield(key); } else if (argc == 1) { VALUE desc = rb_protect(rb_inspect, key, 0); if (NIL_P(desc)) { desc = rb_any_to_s(key); } desc = rb_str_ellipsize(desc, 65); rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key); } else { return argv[1]; } } }
Returns a new ::Array
containing the values associated with the given keys *keys:
h = {foo: 0, bar: 1, baz: 2}
h.fetch_values(:baz, :foo) # => [2, 0]
Returns a new empty ::Array
if no arguments given.
When a block is given, calls the block with each missing key, treating the block’s return value as the value for that key:
h = {foo: 0, bar: 1, baz: 2}
values = h.fetch_values(:, :foo, :bad, :bam) {|key| key.to_s}
values # => [1, 0, "bad", "bam"]
When no block is given, raises an exception if any given key is not found.
# File 'hash.c', line 2742
static VALUE rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash) { VALUE result = rb_ary_new2(argc); long i; for (i=0; i<argc; i++) { rb_ary_push(result, rb_hash_fetch(hash, argv[i])); } return result; }
#select {|key, value| ... } ⇒ Hash
#select ⇒ Enumerator
Also known as: #select
Hash
#select ⇒ Enumerator
Returns a new Hash object whose entries are those for which the block returns a truthy value:
h = {foo: 0, bar: 1, baz: 2}
h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
# File 'hash.c', line 2779
static VALUE rb_hash_select(VALUE hash) { VALUE result; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); result = hash_dup_with_compare_by_id(hash); if (!RHASH_EMPTY_P(hash)) { rb_hash_foreach(result, keep_if_i, result); compact_after_delete(result); } return result; }
#select! {|key, value| ... } ⇒ self
?
#select! ⇒ Enumerator
Also known as: #select!
self
?
#select! ⇒ Enumerator
Returns self
, whose entries are those for which the block returns a truthy value:
h = {foo: 0, bar: 1, baz: 2}
h.select! {|key, value| value < 2 } => {:foo=>0, :=>1}
Returns nil
if no entries were removed.
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
# File 'hash.c', line 2810
static VALUE rb_hash_select_bang(VALUE hash) { st_index_t n; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify_check(hash); n = RHASH_SIZE(hash); if (!n) return Qnil; rb_hash_foreach(hash, keep_if_i, hash); if (n == RHASH_SIZE(hash)) return Qnil; return hash; }
Returns a new ::Array
object that is a 1-dimensional flattening of self
.
By default, nested Arrays are not flattened:
h = {foo: 0, bar: [:bat, 3], baz: 2}
h.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]
Takes the depth of recursive flattening from ::Integer
argument level
:
h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
h.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
h.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
h.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
h.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]
When level
is negative, flattens all nested Arrays:
h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
h.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
h.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]
When level
is zero, returns the equivalent of #to_a :
h = {foo: 0, bar: [:bat, 3], baz: 2}
h.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
h.flatten(0) == h.to_a # => true
# File 'hash.c', line 4254
static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash) { VALUE ary; rb_check_arity(argc, 0, 1); if (argc) { int level = NUM2INT(argv[0]); if (level == 0) return rb_hash_to_a(hash); ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2); rb_hash_foreach(hash, flatten_i, ary); level--; if (level > 0) { VALUE ary_flatten_level = INT2FIX(level); rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level); } else if (level < 0) { /* flatten recursively */ rb_funcallv(ary, id_flatten_bang, 0, 0); } } else { ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2); rb_hash_foreach(hash, flatten_i, ary); } return ary; }
#include?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Alias for #key?.
#has_value?(value) ⇒ Boolean
#value?(value) ⇒ Boolean
Boolean
#value?(value) ⇒ Boolean
Alias for #value?.
#hash ⇒ Integer
# File 'hash.c', line 3848
static VALUE rb_hash_hash(VALUE hash) { st_index_t size = RHASH_SIZE(hash); st_index_t hval = rb_hash_start(size); hval = rb_hash_uint(hval, (st_index_t)rb_hash_hash); if (size) { rb_hash_foreach(hash, hash_i, (VALUE)&hval); } hval = rb_hash_end(hval); return ST2FIX(hval); }
#include?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Alias for #key?.
#replace(other_hash) ⇒ self
#initialize_copy(other_hash) ⇒ self
self
#initialize_copy(other_hash) ⇒ self
Alias for #replace.
Alias for #to_s.
#invert ⇒ Hash
Returns a new Hash object with the each key-value pair inverted:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.invert
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
Overwrites any repeated new keys: (see Entry Order
):
h = {foo: 0, bar: 0, baz: 0}
h.invert # => {0=>:baz}
# File 'hash.c', line 3883
static VALUE rb_hash_invert(VALUE hash) { VALUE h = rb_hash_new_with_size(RHASH_SIZE(hash)); rb_hash_foreach(hash, rb_hash_invert_i, h); return h; }
#keep_if {|key, value| ... } ⇒ self
#keep_if ⇒ Enumerator
self
#keep_if ⇒ Enumerator
Calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry; returns self
.
h = {foo: 0, bar: 1, baz: 2}
h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
# File 'hash.c', line 2841
static VALUE rb_hash_keep_if(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify_check(hash); if (!RHASH_TABLE_EMPTY_P(hash)) { rb_hash_foreach(hash, keep_if_i, hash); } return hash; }
#key(value) ⇒ key?
Returns the key for the first-found entry with the given value
(see Entry Order
):
h = {foo: 0, bar: 2, baz: 2}
h.key(0) # => :foo
h.key(2) # => :bar
Returns nil
if no such value is found.
# File 'hash.c', line 2351
static VALUE rb_hash_key(VALUE hash, VALUE value) { VALUE args[2]; args[0] = value; args[1] = Qnil; rb_hash_foreach(hash, key_i, (VALUE)args); return args[1]; }
#include?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Also known as: #include?, #member?, #has_key?
Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Returns true
if #key is a key in self
, otherwise false
.
# File 'hash.c', line 3654
VALUE rb_hash_has_key(VALUE hash, VALUE key) { return RBOOL(hash_stlike_lookup(hash, key, NULL)); }
#keys ⇒ Array
Returns a new ::Array
containing all keys in self
:
h = {foo: 0, bar: 1, baz: 2}
h.keys # => [:foo, :bar, :baz]
# File 'hash.c', line 3567
VALUE rb_hash_keys(VALUE hash) { st_index_t size = RHASH_SIZE(hash); VALUE keys = rb_ary_new_capa(size); if (size == 0) return keys; if (ST_DATA_COMPATIBLE_P(VALUE)) { RARRAY_PTR_USE(keys, ptr, { if (RHASH_AR_TABLE_P(hash)) { size = ar_keys(hash, ptr, size); } else { st_table *table = RHASH_ST_TABLE(hash); size = st_keys(table, ptr, size); } }); rb_gc_writebarrier_remember(keys); rb_ary_set_len(keys, size); } else { rb_hash_foreach(hash, keys_i, keys); } return keys; }
Also known as: #size
Returns the count of entries in self
:
{foo: 0, bar: 1, baz: 2}.length # => 3
# File 'hash.c', line 2999
VALUE rb_hash_size(VALUE hash) { return INT2FIX(RHASH_SIZE(hash)); }
#include?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Boolean
#has_key?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
Alias for #key?.
#merge ⇒ copy_of_self
#merge(*other_hashes) ⇒ Hash
#merge(*other_hashes) {|key, old_value, new_value| ... } ⇒ Hash
copy_of_self
#merge(*other_hashes) ⇒ Hash
#merge(*other_hashes) {|key, old_value, new_value| ... } ⇒ Hash
Returns the new Hash formed by merging each of other_hashes
into a copy of self
.
Each argument in other_hashes
must be a Hash.
With arguments and no block:
-
Returns the new Hash object formed by merging each successive Hash in
other_hashes
intoself
. -
Each new-key entry is added at the end.
-
Each duplicate-key entry’s value overwrites the previous value.
Example:
h = {foo: 0, bar: 1, baz: 2}
h1 = {bat: 3, bar: 4}
h2 = {bam: 5, bat:6}
h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
With arguments and a block:
-
Returns a new Hash object that is the merge of
self
and each given hash. -
The given hashes are merged left to right.
-
Each new-key entry is added at the end.
-
For each duplicate key:
-
Calls the block with the key and the old and new values.
-
The block’s return value becomes the new value for the entry.
-
Example:
h = {foo: 0, bar: 1, baz: 2}
h1 = {bat: 3, bar: 4}
h2 = {bam: 5, bat:6}
h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
With no arguments:
-
Returns a copy of
self
. -
The block, if given, is ignored.
Example:
h = {foo: 0, bar: 1, baz: 2}
h.merge # => {:foo=>0, :bar=>1, :baz=>2}
h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
h1 # => {:foo=>0, :bar=>1, :baz=>2}
# File 'hash.c', line 4090
static VALUE rb_hash_merge(int argc, VALUE *argv, VALUE self) { return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self)); }
#merge! ⇒ self
#merge!(*other_hashes) ⇒ self
#merge!(*other_hashes) {|key, old_value, new_value| ... } ⇒ self
Also known as: #update
self
#merge!(*other_hashes) ⇒ self
#merge!(*other_hashes) {|key, old_value, new_value| ... } ⇒ self
Merges each of other_hashes
into self
; returns self
.
Each argument in other_hashes
must be a Hash.
With arguments and no block:
-
Returns
self
, after the given hashes are merged into it. -
The given hashes are merged left to right.
-
Each new entry is added at the end.
-
Each duplicate-key entry’s value overwrites the previous value.
Example:
h = {foo: 0, bar: 1, baz: 2}
h1 = {bat: 3, bar: 4}
h2 = {bam: 5, bat:6}
h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
With arguments and a block:
-
Returns
self
, after the given hashes are merged. -
The given hashes are merged left to right.
-
Each new-key entry is added at the end.
-
For each duplicate key:
-
Calls the block with the key and the old and new values.
-
The block’s return value becomes the new value for the entry.
-
Example:
h = {foo: 0, bar: 1, baz: 2}
h1 = {bat: 3, bar: 4}
h2 = {bam: 5, bat:6}
h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
With no arguments:
-
Returns
self
, unmodified. -
The block, if given, is ignored.
Example:
h = {foo: 0, bar: 1, baz: 2}
h.merge # => {:foo=>0, :bar=>1, :baz=>2}
h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
h1 # => {:foo=>0, :bar=>1, :baz=>2}
# File 'hash.c', line 3971
static VALUE rb_hash_update(int argc, VALUE *argv, VALUE self) { int i; bool block_given = rb_block_given_p(); rb_hash_modify(self); for (i = 0; i < argc; i++){ VALUE hash = to_hash(argv[i]); if (block_given) { rb_hash_foreach(hash, rb_hash_update_block_i, self); } else { rb_hash_foreach(hash, rb_hash_update_i, self); } } return self; }
#rassoc(value) ⇒ Array?
# File 'hash.c', line 4200
static VALUE rb_hash_rassoc(VALUE hash, VALUE obj) { VALUE args[2]; args[0] = obj; args[1] = Qnil; rb_hash_foreach(hash, rassoc_i, (VALUE)args); return args[1]; }
#rehash ⇒ self
Rebuilds the hash table by recomputing the hash index for each key; returns self
.
The hash table becomes invalid if the hash value of a key has changed after the entry was created. See Modifying an Active Hash Key
.
# File 'hash.c', line 2012
VALUE rb_hash_rehash(VALUE hash) { VALUE tmp; st_table *tbl; if (hash_iterating_p(hash)) { rb_raise(rb_eRuntimeError, "rehash during iteration"); } rb_hash_modify_check(hash); if (RHASH_AR_TABLE_P(hash)) { tmp = hash_alloc(0); rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); hash_ar_free_and_clear_table(hash); ar_copy(hash, tmp); } else if (RHASH_ST_TABLE_P(hash)) { st_table *old_tab = RHASH_ST_TABLE(hash); tmp = hash_alloc(0); hash_st_table_init(tmp, old_tab->type, old_tab->num_entries); tbl = RHASH_ST_TABLE(tmp); rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); hash_st_free(hash); RHASH_ST_TABLE_SET(hash, tbl); RHASH_ST_CLEAR(tmp); } hash_verify(hash); return hash; }
#reject {|key, value| ... } ⇒ Hash
#reject ⇒ Enumerator
Hash
#reject ⇒ Enumerator
Returns a new Hash object whose entries are all those from self
for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.reject {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
h1 = e.each {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
# File 'hash.c', line 2623
static VALUE rb_hash_reject(VALUE hash) { VALUE result; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); result = hash_dup_with_compare_by_id(hash); if (!RHASH_EMPTY_P(hash)) { rb_hash_foreach(result, delete_if_i, result); compact_after_delete(result); } return result; }
#reject! {|key, value| ... } ⇒ self
?
#reject! ⇒ Enumerator
self
?
#reject! ⇒ Enumerator
Returns self
, whose remaining entries are those for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2}
h.reject! {|key, value| value < 2 } # => {:baz=>2}
Returns nil
if no entries are removed.
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
# File 'hash.c', line 2591
static VALUE rb_hash_reject_bang(VALUE hash) { st_index_t n; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify(hash); n = RHASH_SIZE(hash); if (!n) return Qnil; rb_hash_foreach(hash, delete_if_i, hash); if (n == RHASH_SIZE(hash)) return Qnil; return hash; }
#replace(other_hash) ⇒ self
Also known as: #initialize_copy
Replaces the entire contents of self
with the contents of other_hash
; returns self
:
h = {foo: 0, bar: 1, baz: 2}
h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
# File 'hash.c', line 2964
static VALUE rb_hash_replace(VALUE hash, VALUE hash2) { rb_hash_modify_check(hash); if (hash == hash2) return hash; if (hash_iterating_p(hash)) { rb_raise(rb_eRuntimeError, "can't replace hash during iteration"); } hash2 = to_hash(hash2); COPY_DEFAULT(hash, hash2); if (RHASH_AR_TABLE_P(hash)) { hash_ar_free_and_clear_table(hash); } else { hash_st_free_and_clear_table(hash); } hash_copy(hash, hash2); return hash; }
#select {|key, value| ... } ⇒ Hash
#select ⇒ Enumerator
Hash
#select ⇒ Enumerator
Alias for #filter.
#select! {|key, value| ... } ⇒ self
?
#select! ⇒ Enumerator
self
?
#select! ⇒ Enumerator
Alias for #filter!.
#shift ⇒ Array, value
Removes the first hash entry (see Entry Order
); returns a 2-element ::Array
containing the removed key and value:
h = {foo: 0, bar: 1, baz: 2}
h.shift # => [:foo, 0]
h # => {:bar=>1, :baz=>2}
Returns nil if the hash is empty.
# File 'hash.c', line 2489
static VALUE rb_hash_shift(VALUE hash) { struct shift_var var; rb_hash_modify_check(hash); if (RHASH_AR_TABLE_P(hash)) { var.key = Qundef; if (!hash_iterating_p(hash)) { if (ar_shift(hash, &var.key, &var.val)) { return rb_assoc_new(var.key, var.val); } } else { rb_hash_foreach(hash, shift_i_safe, (VALUE)&var); if (!UNDEF_P(var.key)) { rb_hash_delete_entry(hash, var.key); return rb_assoc_new(var.key, var.val); } } } if (RHASH_ST_TABLE_P(hash)) { var.key = Qundef; if (!hash_iterating_p(hash)) { if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) { return rb_assoc_new(var.key, var.val); } } else { rb_hash_foreach(hash, shift_i_safe, (VALUE)&var); if (!UNDEF_P(var.key)) { rb_hash_delete_entry(hash, var.key); return rb_assoc_new(var.key, var.val); } } } return Qnil; }
Alias for #length.
#slice(*keys) ⇒ Hash
# File 'hash.c', line 2648
static VALUE rb_hash_slice(int argc, VALUE *argv, VALUE hash) { int i; VALUE key, value, result; if (argc == 0 || RHASH_EMPTY_P(hash)) { return copy_compare_by_id(rb_hash_new(), hash); } result = copy_compare_by_id(rb_hash_new_with_size(argc), hash); for (i = 0; i < argc; i++) { key = argv[i]; value = rb_hash_lookup2(hash, key, Qundef); if (!UNDEF_P(value)) rb_hash_aset(result, key, value); } return result; }
#[]=(key, value) ⇒ value
#store(key, value)
value
#store(key, value)
Alias for #[]=.
#to_a ⇒ Array
# File 'hash.c', line 3411
static VALUE rb_hash_to_a(VALUE hash) { VALUE ary; ary = rb_ary_new_capa(RHASH_SIZE(hash)); rb_hash_foreach(hash, to_a_i, ary); return ary; }
#to_h ⇒ self
, Hash
#to_h {|key, value| ... } ⇒ Hash
self
, Hash
#to_h {|key, value| ... } ⇒ Hash
For an instance of Hash, returns self
.
For a subclass of Hash, returns a new Hash containing the content of self
.
When a block is given, returns a new Hash object whose content is based on the block; the block should return a 2-element ::Array
object specifying the key-value pair to be included in the returned ::Array
:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.to_h {|key, value| [value, key] }
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
# File 'hash.c', line 3538
static VALUE rb_hash_to_h(VALUE hash) { if (rb_block_given_p()) { return rb_hash_to_h_block(hash); } if (rb_obj_class(hash) != rb_cHash) { const VALUE flags = RBASIC(hash)->flags; hash = hash_dup(hash, rb_cHash, flags & RHASH_PROC_DEFAULT); } return hash; }
#to_hash ⇒ self
Returns self
.
# File 'hash.c', line 3480
static VALUE rb_hash_to_hash(VALUE hash) { return hash; }
#to_proc ⇒ Proc
Returns a ::Proc
object that maps a key to its value:
h = {foo: 0, bar: 1, baz: 2}
proc = h.to_proc
proc.class # => Proc
proc.call(:foo) # => 0
proc.call(: ) # => 1
proc.call(:nosuch) # => nil
# File 'hash.c', line 4698
static VALUE rb_hash_to_proc(VALUE hash) { return rb_func_lambda_new(hash_proc_call, hash, 1, 1); }
#to_s ⇒ String Also known as: #inspect
# File 'hash.c', line 3466
static VALUE rb_hash_inspect(VALUE hash) { if (RHASH_EMPTY_P(hash)) return rb_usascii_str_new2("{}"); return rb_exec_recursive(inspect_hash, hash, 0); }
#transform_keys {|key| ... } ⇒ Hash
#transform_keys(hash2) ⇒ Hash
#transform_keys(hash2) {|other_key| ... } ⇒ Hash
#transform_keys ⇒ Enumerator
Hash
#transform_keys(hash2) ⇒ Hash
#transform_keys(hash2) {|other_key| ... } ⇒ Hash
#transform_keys ⇒ Enumerator
Returns a new Hash object; each entry has:
-
A key provided by the block.
-
The value from
self
.
An optional hash argument can be provided to map keys to new keys. Any key not given will be mapped using the provided block, or remain the same if no block is given.
Transform keys:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.transform_keys {|key| key.to_s }
h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
h.transform_keys(foo: :, bar: :foo)
#=> {bar: 0, foo: 1, baz: 2}
h.transform_keys(foo: :hello, &:to_s)
#=> {:hello=>0, "bar"=>1, "baz"=>2}
Overwrites values for duplicate keys:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.transform_keys {|key| :bat }
h1 # => {:bat=>2}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.transform_keys # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_keys>
h1 = e.each { |key| key.to_s }
h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
# File 'hash.c', line 3224
static VALUE rb_hash_transform_keys(int argc, VALUE *argv, VALUE hash) { VALUE result; struct transform_keys_args transarg = {0}; argc = rb_check_arity(argc, 0, 1); if (argc > 0) { transarg.trans = to_hash(argv[0]); transarg.block_given = rb_block_given_p(); } else { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); } result = rb_hash_new(); if (!RHASH_EMPTY_P(hash)) { if (transarg.trans) { transarg.result = result; rb_hash_foreach(hash, transform_keys_hash_i, (VALUE)&transarg); } else { rb_hash_foreach(hash, transform_keys_i, result); } } return result; }
#transform_keys! {|key| ... } ⇒ self
#transform_keys!(hash2) ⇒ self
#transform_keys!(hash2) {|other_key| ... } ⇒ self
#transform_keys! ⇒ Enumerator
self
#transform_keys!(hash2) ⇒ self
#transform_keys!(hash2) {|other_key| ... } ⇒ self
#transform_keys! ⇒ Enumerator
Same as #transform_keys but modifies the receiver in place instead of returning a new hash.
# File 'hash.c', line 3264
static VALUE rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash) { VALUE trans = 0; int block_given = 0; argc = rb_check_arity(argc, 0, 1); if (argc > 0) { trans = to_hash(argv[0]); block_given = rb_block_given_p(); } else { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); } rb_hash_modify_check(hash); if (!RHASH_TABLE_EMPTY_P(hash)) { long i; VALUE new_keys = hash_alloc(0); VALUE pairs = rb_ary_hidden_new(RHASH_SIZE(hash) * 2); rb_hash_foreach(hash, flatten_i, pairs); for (i = 0; i < RARRAY_LEN(pairs); i += 2) { VALUE key = RARRAY_AREF(pairs, i), new_key, val; if (!trans) { new_key = rb_yield(key); } else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key, Qundef))) { /* use the transformed key */ } else if (block_given) { new_key = rb_yield(key); } else { new_key = key; } val = RARRAY_AREF(pairs, i+1); if (!hash_stlike_lookup(new_keys, key, NULL)) { rb_hash_stlike_delete(hash, &key, NULL); } rb_hash_aset(hash, new_key, val); rb_hash_aset(new_keys, new_key, Qnil); } rb_ary_clear(pairs); rb_hash_clear(new_keys); } compact_after_delete(hash); return hash; }
#transform_values {|value| ... } ⇒ Hash
#transform_values ⇒ Enumerator
Hash
#transform_values ⇒ Enumerator
Returns a new Hash object; each entry has:
-
A key from
self
. -
A value provided by the block.
Transform values:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.transform_values {|value| value * 100}
h1 # => {:foo=>0, :bar=>100, :baz=>200}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
h1 = e.each { |value| value * 100}
h1 # => {:foo=>0, :bar=>100, :baz=>200}
# File 'hash.c', line 3349
static VALUE rb_hash_transform_values(VALUE hash) { VALUE result; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); result = hash_dup_with_compare_by_id(hash); SET_DEFAULT(result, Qnil); if (!RHASH_EMPTY_P(hash)) { rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result); compact_after_delete(result); } return result; }
#transform_values! {|value| ... } ⇒ self
#transform_values! ⇒ Enumerator
self
#transform_values! ⇒ Enumerator
Returns self
, whose keys are unchanged, and whose values are determined by the given block.
h = {foo: 0, bar: 1, baz: 2}
h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
Returns a new ::Enumerator
if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!>
h1 = e.each {|value| value * 100}
h1 # => {:foo=>0, :bar=>100, :baz=>200}
# File 'hash.c', line 3381
static VALUE rb_hash_transform_values_bang(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); rb_hash_modify_check(hash); if (!RHASH_TABLE_EMPTY_P(hash)) { rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash); } return hash; }
#merge! ⇒ self
#merge!(*other_hashes) ⇒ self
#merge!(*other_hashes) {|key, old_value, new_value| ... } ⇒ self
self
#merge!(*other_hashes) ⇒ self
#merge!(*other_hashes) {|key, old_value, new_value| ... } ⇒ self
Alias for #merge!.
#has_value?(value) ⇒ Boolean
#value?(value) ⇒ Boolean
Also known as: #has_value?
Boolean
#value?(value) ⇒ Boolean
Returns true
if value
is a value in self
, otherwise false
.
# File 'hash.c', line 3680
static VALUE rb_hash_has_value(VALUE hash, VALUE val) { VALUE data[2]; data[0] = Qfalse; data[1] = val; rb_hash_foreach(hash, rb_hash_search_value, (VALUE)data); return data[0]; }
#values ⇒ Array
Returns a new ::Array
containing all values in self
:
h = {foo: 0, bar: 1, baz: 2}
h.values # => [0, 1, 2]
# File 'hash.c', line 3611
VALUE rb_hash_values(VALUE hash) { VALUE values; st_index_t size = RHASH_SIZE(hash); values = rb_ary_new_capa(size); if (size == 0) return values; if (ST_DATA_COMPATIBLE_P(VALUE)) { if (RHASH_AR_TABLE_P(hash)) { rb_gc_writebarrier_remember(values); RARRAY_PTR_USE(values, ptr, { size = ar_values(hash, ptr, size); }); } else if (RHASH_ST_TABLE_P(hash)) { st_table *table = RHASH_ST_TABLE(hash); rb_gc_writebarrier_remember(values); RARRAY_PTR_USE(values, ptr, { size = st_values(table, ptr, size); }); } rb_ary_set_len(values, size); } else { rb_hash_foreach(hash, values_i, values); } return values; }
#values_at(*keys) ⇒ Array
# File 'hash.c', line 2710
static VALUE rb_hash_values_at(int argc, VALUE *argv, VALUE hash) { VALUE result = rb_ary_new2(argc); long i; for (i=0; i<argc; i++) { rb_ary_push(result, rb_hash_aref(hash, argv[i])); } return result; }