Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Few Important Hash Methods in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 262
    Comment on it

    Hash:

    Hashes are way of storing data in key-value pair format. Key can be any ruby object ( i.e. string, numbers ). Here we will see some of mostly used methods for hashes.

    1) Creating/Initializing a hash:

    Hash can be created in different ways like these:

    ## using new keyword
    my_hash = Hash.new
    
    ## using {}
    my_hash = {}
    
    ## By passing default values:
    my_hash = Hash.new "default value"

    So the difference between initializing the hash with a default value and without a default value is that, in case of default value, if any key's value is accessed which is not present, one will return the default value provided and the other will return nil.

    > my_hash = Hash.new
    => {}
    > my_hash_default = Hash.new "default value"
    => {}
    > my_hash["some_key"]
    => nil
    > my_hash_default["some_key"]
    => "default value"

    2) Fetching all keys of hash:

    To get all keys of a hash in an array the keys method is used on a hash object.

    > my_hash = {key1: "value1", key2: "value2", key3: "value3"}
    => {:key1=>"value1", :key2=>"value2", :key3=>"value3"}
    
    ## To retreive all the keys
    > my_hash.keys
    => [:key1, :key2, :key3]


    3) Remove all key value pair from hash:

    To remove all key value pair from the hash clear method is used:

    > my_hash = {key1: "value1", key2: "value2", key3: "value3"}
    => {:key1=>"value1", :key2=>"value2", :key3=>"value3"}
    > my_hash.clear
    => {}
    > my_hash
    => {}

    4) Deleting a key value pair from hash:

    You can delete a key value pair from hash using the key or by providing a specific condition with the help of delete method.

    > my_hash = {key1: 2, key2: 3, key3: 4, key4: 5, key6: 6}
    => {:key1=>2, :key2=>3, :key3=>4, :key4=>5, :key6=>6}
    ## To delete the key2 key-value pair:
    > my_hash.delete(:key1)
    => 2
    ## Now check my_hash the key1 key-value pair has been removed from my_hash
    > my_hash
    => {:key2=>3, :key3=>4, :key4=>5, :key6=>6}
    
    ## To delete all the even number delete_if can be used liked this
    
    > my_hash = {key1: 2, key2: 3, key3: 4, key4: 5, key6: 6}
    => {:key1=>2, :key2=>3, :key3=>4, :key4=>5, :key6=>6}
    > my_hash.delete_if { |key,value| value%2==0 }
    => {:key2=>3, :key4=>5}

     

    5) Iterating over hash:

    Their are different ways for iterating over an hash:

    ## Iterating over hash by using key-value pair as a parameter for the block
    
    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    
    > my_hash.each{|key, value| puts "Key is : #{key}  Value is : #{value}" }
    Key is : 1  Value is : 10
    Key is : 2  Value is : 20
    Key is : 3  Value is : 30
    Key is : 4  Value is : 40
    Key is : 5  Value is : 50
     => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50}
    
    ## Iterating over hash by using only key as a parameter for the block
    
    > my_hash.each_key { |key| puts "Key is #{key}" }
    Key is 1
    Key is 2
    Key is 3
    Key is 4
    Key is 5
     => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50}
    
    
    ## Iterating over hash by using only value as a parameter for the block
    > my_hash.each_value { |value| puts "Value is #{value}" }
    Value is 10
    Value is 20
    Value is 30
    Value is 40
    Value is 50
     => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50}


    6) Fetching all values of a hash in an array:

    To fetch all the values of a hash in an array values method is used:

    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    > my_hash.values
    => [10, 20, 30, 40, 50]

     

    7) To check whether a key is present in hash or not:

    There are different methods available that returns true or false to tell whether the given key is present in the hash or not:
     

    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    
    ## Using has_key? method
    > my_hash.has_key?("1")
     => true
    > my_hash.has_key?("6")
     => false
    
    ## Using include? method
    > my_hash.include?("1")
    => true
    
    ## Using key? method
    > my_hash.key?("1")
    => true
    
    ## Using member? method
    > my_hash.member?("1")
    => true
    
    8) Test whether hash contains some value
    
    > my_hash.value?(20)
    => true

     

    9) Inverting the key value pairs of a hash:

    If you want to invert the key value pairs of the hash you can use invert method. Now in the new hash the key will become values and the values will become keys:

    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    > my_hash.invert
    => {10=>"1", 20=>"2", 30=>"3", 40=>"4", 50=>"5"}

     

    10) Merging two hashes:

    Suppose we have two hashes and we want to create a new hash that has all the keys and values of both the hashes, we can use merge
     

    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50}
    
    > my_hash2 = {"6" => 60, "7" => 70}
    => {"6"=>60, "7"=>70}
    
    my_hash.merge(my_hash2)
    => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50, "6"=>60, "7"=>70}

     

    11) Get the length of hash:

    To get the number of key value pairs of hash length or size method can be used:

    > my_hash = {"1" => 10, "2" => 20, "3" => 30, "4" => 40, "5" => 50}
    => {"1"=>10, "2"=>20, "3"=>30, "4"=>40, "5"=>50}
    
    > my_hash.length
     => 5
    > my_hash.size
     => 5

    12) Select, reject and replace methods:

    Select is used to create a new hash from the existing hash by selecting key, value pairs based on some conditions:

    >  my_hash = {"1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5}
    => {"1"=>1, "2"=>2, "3"=>3, "4"=>4, "5"=>5}
    
    ## To select members who has even values
    > my_hash.select {|key, value| value%2==0 }
    => {"2"=>2, "4"=>4}


    Reject is just opposite to select, it just removes those elements that specifies some condition:

    ## To select members who has odd values
    > my_hash.reject {|key, value| value%2==0 }
    => {"1"=>1, "3"=>3, "5"=>5}

    Replace is used to replace the elements of one hash with other hash

    > my_hash = {"1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5}
    => {"1"=>1, "2"=>2, "3"=>3, "4"=>4, "5"=>5}
    > my_hash2 = {"3" => 300, "4" => 400}
    => {"3"=>300, "4"=>400}
    > my_hash.replace(my_hash2)
    => {"3"=>300, "4"=>400}

    Hope you liked reading this blog.     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: