Home » Ruby programming

Hash Creation in Ruby

Ruby | Hash Creation: In this tutorial, we are going to learn about the Hash, how to create hash, accessing hash keys, values, etc with examples.
Submitted by Hrithik Chandra Prasad, on February 15, 2020

What are hashes?

Before moving further, it is very necessary to understand the actual meaning of Hash objects. A hash is a collection of unique keys along with their values that are is very similar to a dictionary. They are also known as associative arrays. Instead of numbers as indexes, they use objects.

There are certain ways to create a Hash. Most of the ways are given in the rest of the article.

Method 1:

A hash can be created in an implicit way in the following manner.

Syntax:

    Hash_object = {"Key"=> value, "Key"=> value}

Example:

=begin
  Ruby program to demonstrate Hash creation
=end	

hsh = {"colors"  => "red",
     "letters" => "a", "Fruit" => "Grapes"}
     
puts "Hash contents are : #{hsh}"

Output

Hash contents are : {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes"}

Method 2:

You can create an instance of Hash class in the following way as well.

Syntax:

    Hash_object = {:Key=> value, :Key:=> value}

Example:

=begin
  Ruby program to demonstrate Hash creation
=end	

hsh = {:colors  => "red",
     :letters => "a", :Fruit => "Grapes", :Love=> "Self", :vege=>"Potato"}
     
puts "Hash contents are : #{hsh}"

Output

Hash contents are : {:colors=>"red", :letters=>"a", :Fruit=>"Grapes", :Love=>"Self", :vege=>"Potato"}

Method 3:

You can create a Hash object with the help of new keyword in the following way.

Syntax:

    Hash_object = Hash.new

Example:

=begin
  Ruby program to demonstrate Hash creation
=end	

hsh = Hash.new 

hsh["color"] = "Black"
hsh["age"] = 20
hsh["car"] = "Wrv"
hsh["home"] = "mom"
hsh["college"]= "geu"

puts "Hash contents are : #{hsh}"

Output

Hash contents are : {"color"=>"Black", "age"=>20, "car"=>"Wrv", "home"=>"mom", "college"=>"geu"}

Method 4:

You can create a Hash object by passing the default value in the new method of Hash class at the time of creating the Hash. This will help you in the way that the default value will be printed as the output when the key is not present in the Hash object which is being demanded by the user. If no default value is set then 'nil' will be returned from the method. The default value can be set by passing the value as an argument inside the new method. You can also set it by using the method "default". The syntaxes for both are given below.

Syntax:

    Hash_object = Hash.new(default_value)
    or
    Hash_object = {"Key"=> value, "Key"=> value}
    Hash_object.default = "default_value"

Example 1:

=begin
  Ruby program to demonstrate Hash creation
=end	

hsh = Hash.new(0) 

hsh["color"] = "Black"
hsh["age"] = 20
hsh["car"] = "Wrv"
hsh["home"] = "mom"
hsh["college"]= "geu"

puts "Hash contents are : #{hsh}"
puts hsh["game"]

Output

Hash contents are : {"color"=>"Black", "age"=>20, "car"=>"Wrv", "home"=>"mom", "college"=>"geu"}
0

Explanation:

You can see that the value 0 is returned when the value of "game" key was demanded because it does not exist in the Hash.

Example 2:

=begin
  Ruby program to demonstrate Hash creation
=end	

hsh = Hash.new(0) 

hsh["color"] = "Black"
hsh["age"] = 20

hsh.default = "Not found"

puts "Hash contents are : #{hsh}"
puts hsh["game"]

Output

Hash contents are : {"color"=>"Black", "age"=>20}
Not found

Explanation:

You can observe that the default value can be passed with the help of the default method also which is already present inside the Hash class.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.