×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Hash Creation in Ruby

By IncludeHelp Last updated : December 01, 2024

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.

Implicit Way to Create a Hash

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"}

Create a Hash Using Instance of Hash Class

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"}

Create a Hash Using new Keyword

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"}

Create a Hash with Default Value

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.