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
valueassociated withkey.h[key]=valueAssociates
valuewithkey.h.clearDeletes 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.defaultReturns 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=valueSets 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access