Accessing the entries of a Hash using the Hash.keys method
The method returns an array containing all the keys of the hash
Syntax:
hash.keys
Example: In this example, we use hash.keys method to return an array containing all the keys of the hash
# Define a hash
hash = { "a" => 1, "b" => 2, "c" => 3 }
# Access keys using keys method
keys = hash.keys
puts keys.inspect # Output: ["a", "b", "c"]
Output
{"name"=>"John", "age"=>30, "city"=>"New York"}
How can we access the entries of a Hash in Ruby?
In this article, we will discuss how to access the entries of a Hash in ruby. We can access the entries of a Hash through different methods ranging from using keys, and values to each method
Table of Content
- Accessing the entries of a Hash using Hash.keys method
- Accessing the entries of a Hash using Hash.values method
- Accessing the entries of a Hash using Hash.each method
Contact Us