Home » Ruby programming

Hash.keys Method with Example in Ruby

Ruby Hash.keys Method: Here, we are going to learn about the Hash.keys Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on March 12, 2020

Hash.keys Method

In this article, we will study about Hash.keys Method. The working of the method can't be assumed because of it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

Method description:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns an array contacting all the keys which are present in the hash object. In simpler language, you can say that the new array will be containing the keys from the self hash object. It will return an empty array if no values are present in the hash objects.

Syntax:

    Hash.keys

Example 1:

=begin
  Ruby program to demonstrate Hash.keys method
=end	

hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}

puts "Hash.keys implementation:"
ary = hsh.keys

puts "The keys present in the hash are: #{ary}"

puts "Self hash object: #{hsh}"

Output

Hash.keys implementation:
The keys present in the hash are: ["colors", "letters", "Fruit", "anything", "sweet"]
Self hash object: {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo"}

Explanation:

In the above code, you can observe that you can find all the keys present in the hash with the help of the Hash.keys method. The array instance returned from the hash will be populated with all the keys present in the specific hash object. This is very clear with the help of the output drawn from the program code given for the demonstration purpose.

Example 2:

=begin
  Ruby program to demonstrate Hash.keys method
=end	

hsh = Hash.new()

puts "Hash.keys implementation:"

ary = hsh.keys

puts "The keys present in the hash are: #{ary}"

puts "Self hash object: #{hsh}"

Output

Hash.keys implementation:
The keys present in the hash are: []
Self hash object: {}

Explanation:

In the above code, you can observe that we are trying to fetch all the keys from the hash with the help of the Hash.keys method. This method is returning an empty array if the hash is empty or you can say that it doesn't have any key-value pair. This is very clear with the help of the output drawn by the program code.






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.