Name
Hash — Hash class
Synopsis
Hash
is a
class for collection of key-value pairs, or in other words, a
collection indexed by arbitrary type of objects, which define proper
hash
and eql?
methods.
Included Module
Enumerable
Class Methods
Hash[key, value
...
]
Creates a
Hash
.Hash[1,2,2,4] # => {1=>2, 2=>4}
Hash::new([default=nil])
Creates a
Hash
. A default value may also be specified.h = Hash::new(15) # => {} h[44] # => 15 (no key; default returned)
Instance Methods
Methods of the
Hash
class ending in a pipe !
modify their receiver and return a hash if modification took place,
otherwise nil
. Methods without a
!
return a modified copy of the hash.
h
[
key
]
Returns the
value
associated withkey
.h
[
key
]=
value
Associates
value
withkey
.h
.clear
Deletes all key-value pairs from
h
.h = {1=>2, 2=>4} h.clear h # => {} h = {1=>2, 2=>4} h.delete_if{|k,v| k % 2 == 0} h # => {1=>2}
h
.default
Returns the default value for a key that doesn’t exist. Note that the default value isn’t copied, so that modifying the default object may affect all default values thereafter.
h
.default=
value
Sets the default value.
h
.delete(
key
)
Deletes a key-value pair with a key equal to
key
.h
.delete_if {|
key
,
value
|
...
}
Deletes key-value pairs where the evaluated result of block is
true
.h
.each {|
key
,
value
|
...
}
h
.each_pair {|
key
,
value
|
...
}
Executes the block once for each key-value pair. Pairs are in unspecified order.
h
.each_key {|
key
|
...
}
Executes the block once for each key. Keys are in unspecified order.
h
.each_value {|
value
|
...
}
Executes ...
Get Ruby in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.