Ruby: Hash Class

shay90210

Shay

Posted on March 13, 2024

Ruby: Hash Class

In Ruby, a hash is a data structure that stores key-value pairs. It is similar to an associative array or a dictionary in other programming languages. Hashes are unordered collections of data, where each element is addressed by a unique key.

Hash Overview

Key-Value Pairs: Each element in a hash consists of a key-value pair. The key is unique within the hash and is used to access the corresponding value.

Unordered: Hashes in Ruby are unordered, meaning there's no guarantee of the order in which the key-value pairs are stored internally. The order of insertion is not preserved.

Flexible Keys and Values: Keys and values in a hash can be of any data type, including strings, symbols, integers, or even other hashes.

Dynamic Size: Hashes in Ruby can grow and shrink dynamically as key-value pairs are added or removed.

Fast Lookup: Hashes provide fast lookup times for retrieving values based on keys, making them efficient for tasks like data retrieval and storage.

Common Operations: Hashes support common operations like adding new key-value pairs, deleting key-value pairs, iterating over key-value pairs, merging with other hashes, and more.

Here's an example of a hash in Ruby:

# Creating a hash
my_hash = { "name" => "John", "age" => 30, "city" => "New York" }
Enter fullscreen mode Exit fullscreen mode
# Accessing values using keys
puts my_hash["name"]  # Output: John
puts my_hash["age"]   # Output: 30
puts my_hash["city"]  # Output: New York
Enter fullscreen mode Exit fullscreen mode
# Adding a new key-value pair
my_hash["gender"] = "Male"
Enter fullscreen mode Exit fullscreen mode
# Iterating over key-value pairs
my_hash.each do |key, value|
  puts "#{key}: #{value}"
end
Enter fullscreen mode Exit fullscreen mode
# Output:
# name: John
# age: 30
# city: New York
# gender: Male
Enter fullscreen mode Exit fullscreen mode

In this example, my_hash is a hash containing key-value pairs representing attributes of a person. You can access values using their corresponding keys, add new key-value pairs, iterate over all key-value pairs, and perform other operations typical of hashes in Ruby.

Hash Methods

Hash is a separate class with its own set of methods. While Hashes do inherit some methods from the Enumerable module, they also have their own unique methods specifically tailored for working with key-value pairs.

Here are some examples of methods that are specific to Hash:

[] - To access values by keys.

[]= - To assign values to keys.

each - Iterate over key-value pairs.

keys - Get an array of keys.

values - Get an array of values.

delete - Delete a key-value pair.

fetch - Retrieve a value by key, with an optional default value if the key doesn't exist.

merge - Merge two hashes.

select - Return a new hash containing key-value pairs that satisfy a condition.

has_key? or key? - Check if a key exists in the hash.

has_value? or value? - Check if a value exists in the hash.

invert - Invert keys and values in the hash.

These methods are specifically designed for working with Hashes and are not borrowed from other classes. However, Ruby's Hash class does include methods from the Enumerable module, which provides additional functionality for iterating over collections.

Happy coding!

theGlamTechie

💖 💪 🙅 🚩
shay90210
Shay

Posted on March 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Ruby: Hash Class
programming Ruby: Hash Class

March 13, 2024

Ruby: Methods & Functions
programming Ruby: Methods & Functions

January 24, 2024