Class

Hash

Extends:

Includes:

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order.

Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.

Hash uses key.eql? to test keys for equality. If you need to use instances of your own classes as keys in a Hash, it is recommended that you define both the eql? and hash methods. The hash method must have the property that a.eql?(b) implies a.hash == b.hash.

class MyClass
  attr_reader :str
  def initialize(str)
    @str = str
  end
  def eql?(o)
    o.is_a?(MyClass) && str == o.str
  end
  def hash
    @str.hash
  end
end

a = MyClass.new("some string")
b = MyClass.new("some string")
a.eql? b  #=> true

h = {}

h[a] = 1
h[a]      #=> 1
h[b]      #=> 1

h[b] = 2
h[a]      #=> 2
h[b]      #=> 2
Public Methods
== Equality—Two hashes are equal if they each contain the same number of keys and if each key-value pair is equal to (according to Object#==) the corresponding elements in the other hash.
[] Creates a new hash populated with the given objects. Equivalent to the literal { key, value, … }. Keys and values occur in pairs, so there must be an even number of arguments.
[] Element Reference—Retrieves the value object corresponding to the key object. If not found, returns the a default value (see Hash::new for details).
[]= Element Assignment—Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String passed as a key will be duplicated and frozen).
clear Removes all key-value pairs from hsh.
default Returns the default value, the value that would be returned by hsh[key] if key did not exist in hsh. See also Hash::new and Hash#default=.
default= Sets the default value, the value returned for a key that does not
default_proc If Hash::new was invoked with a block, return that block, otherwise return nil.
delete Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the default value. If the optional code block is given and the key is not found, pass in the key and return the result of block.
delete_if Deletes every key-value pair from hsh for which block evaluates to true.
each Calls block once for each key in hsh, passing the key and value to the block as a two-element array. Because of the assignment semantics of block parameters, these elements will be split out if the block has two formal parameters. Also see Hash.each_pair, which will be marginally more efficient for blocks with two parameters.
each_key Calls block once for each key in hsh, passing the key as a parameter.
each_pair Calls block once for each key in hsh, passing the key and value as parameters.
each_value Calls block once for each key in hsh, passing the value as a parameter.
empty? Returns true if hsh contains no key-value pairs.
fetch Returns a value from the hash for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise an IndexError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
has_key? Returns true if the given key is present in hsh.
has_value? Returns true if the given value is present for some key in hsh.
include? Returns true if the given key is present in hsh.
index Returns the key for a given value. If not found, returns nil.
indexes Deprecated in favor of Hash#select.
indices Deprecated in favor of Hash#select.
initialize_copy Replaces the contents of hsh with the contents of other_hash.
inspect Return the contents of this hash as a string.
invert Returns a new hash created by using hsh’s values as keys, and the keys as values.
key? Returns true if the given key is present in hsh.
keys Returns a new array populated with the keys from this hash. See also Hash#values.
length Returns the number of key-value pairs in the hash.
member? Returns true if the given key is present in hsh.
merge Returns a new hash containing the contents of other_hash and the contents of hsh, overwriting entries in hsh with duplicate keys with those from other_hash.
merge! Adds the contents of other_hash to hsh, overwriting entries with duplicate keys with those from other_hash.
new Returns a new, empty hash. If this hash is subsequently accessed by
rehash Rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted, this method will reindex hsh. If Hash#rehash is called while an iterator is traversing the hash, an IndexError will be raised in the iterator.
reject Same as Hash#delete_if, but works on (and returns) a copy of the hsh. Equivalent to hsh.dup.delete_if.
reject! Equivalent to Hash#delete_if, but returns nil if no changes were made.
replace Replaces the contents of hsh with the contents of other_hash.
select Returns a new array consisting of [key,value] pairs for which the block returns true. Also see Hash.values_at.
shift Removes a key-value pair from hsh and returns it as the two-item array [ key, value ], or the hash’s default value if the hash is empty.
size Returns the number of key-value pairs in the hash.
sort Converts hsh to a nested array of [ key, value ] arrays and sorts it, using Array#sort.
store Element Assignment—Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String passed as a key will be duplicated and frozen).
to_a Converts hsh to a nested array of [ key, value ] arrays.
to_hash Returns self.
to_s Converts hsh to a string by converting the hash to an array of [ key, value ] pairs and then converting that array to a string using Array#join with the default separator.
update Adds the contents of other_hash to hsh, overwriting entries with duplicate keys with those from other_hash.
value? Returns true if the given value is present for some key in hsh.
values Returns a new array populated with the values from hsh. See also Hash#keys.
values_at Return an array containing the values associated with the given keys. Also see Hash.select.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.