Ruby Hashes

A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps.

If a hash is accessed with a key that does not exist, the method will return nil.

Syntax:

name = {"key1" => "value1", "key2" => "value2", "key3" => "value3"...}  

                    OR  

name = {key1:  'value1', key2:  'value2', key3:  'value3'...}

Creating Ruby Hash

Ruby hash is created by writing key-value pair within {} curly braces.

To fetch a hash value, write the required key within [] square bracket.

Example:

color = {   

    "Rose" => "red",   

    "Lily" => "purple",   

    "Marigold" => "yellow",   

    "Jasmine" => "white"   

  }   

  puts color['Rose']   

  puts color['Lily']   

  puts color['Marigold']   

  puts color['Jasmine']

Output:

Ruby hashes 1

Modifying Ruby Hash

A Ruby hash can be modified by adding or removing a key value pair in an already existing hash.

Example:

color = {   

    "Rose" => "red",   

    "Lily" => "purple",   

    "Marigold" => "yellow",   

    "Jasmine" => "white"   

  }   

  color['Tulip'] = "pink"   

  color.each do |key, value|   

  puts "#{key} color is #{value}"   

 end

Output:

Ruby hashes 2

Ruby Hash Methods

A Ruby hash has many methods. Some are public class methods and some public instance methods.

Public Class Methods

MethodDescription
Hash[object]Create a new hash with given objects.
new(obj)Return a new empty hash.
try_convert(obj)Try to convert obj into hash.

Public Instance Methods

MethodDescription
hsh==other_hashTwo hashes are equal if they contain same key and value pair.
hsh[key]Retrieve value from the respective key.
hsh[key] = valueAssociates new value to the given key.
assoc(obj)Compare obj in the hash.
clearRemove all key value pair from hash.
compare_by_identityCompare hash keys by their identity.
compare_by_identity?Return true if hash compare its keys by their identity.
default(key=nil)Return default value.
default = objSets the default value.
delete(key)Delete key value pair.
eachCall block once for each key in hash.
empty?Return true if hash contains no key value pair.
eql>(other)Return true if hash and other both have same content
fetch(key[, default])Return value from hash for a given key.
flattenReturn a new array that is a one-dimensional flattening of this hash.
has_key?(key)Return true if given key is present in hash.
has_value?(value)Return true if given value is present in hash for a key.
include?(key)Return true if given key is present in hash.
to_s/ inspectReturn content of hash as string.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *