DEV Community

Wilbur Suero
Wilbur Suero

Posted on • Originally published at fullstackflow.hashnode.dev on

Hash Maps

A hash map, also known as a dictionary or associative array, is a data structure that stores key-value pairs and provides fast access to the values based on their keys. In Ruby, the hash map data structure is implemented as a hash, which is represented by curly braces {} and each key-value pair is separated by a comma ,.

Let's now look at some examples of hash maps in Ruby.

Example 1: Counting occurrences of elements in an array

In this example, we'll create a hash map to count the occurrences of each element in an array.

# Define the array
arr = [1, 2, 3, 1, 2, 3, 4]

# Create a hash map to count the occurrences of each element
counts = {}
arr.each do |element|
  if counts[element].nil?
    counts[element] = 1
  else
    counts[element] += 1
  end
end

# Print the counts
puts counts # Output: {1=>2, 2=>2, 3=>2, 4=>1}

Enter fullscreen mode Exit fullscreen mode

Example 2: Finding the frequency of words in a string

In this example, we'll create a hash map to find the frequency of words in a string.

# Define the string
str = "Ruby is a dynamic, open source programming language with a focus on simplicity and productivity."

# Create a hash map to find the frequency of words in the string
words = str.split(" ")
frequency = {}
words.each do |word|
  if frequency[word].nil?
    frequency[word] = 1
  else
    frequency[word] += 1
  end
end

# Print the frequency of words
puts frequency # Output: {"Ruby"=>1, "is"=>1, "a"=>2, "dynamic,"=>1, "open"=>1, "source"=>1, "programming"=>1, "language"=>1, "with"=>1, "focus"=>1, "on"=>1, "simplicity"=>1, "and"=>1, "productivity."=>1}

Enter fullscreen mode Exit fullscreen mode

Example 3: Grouping elements of an array by their type

In this example, we'll create a hash map to group the elements of an array by their type.

# Define the array
arr = [1, "Hello", 2.0, [1, 2, 3], {a: 1, b: 2}]

# Create a hash map to group the elements of the array by their type
grouped = {}
arr.each do |element|
  type = element.class
  if grouped[type].nil?
    grouped[type] = [element]
  else
    grouped[type].push(element)
  end
end

# Print the grouped elements
puts grouped # Output: {Fixnum=>[1, 2], String=>["Hello"], Float=>[2.0], Array=>[[1, 2, 3]], Hash=>[{:a=>1, :b=>2}]}

Enter fullscreen mode Exit fullscreen mode

Practical Usage of Hash Maps in Ruby

Hash maps are widely used in Ruby for various purposes such as:

  • Storing configuration settings

  • Counting occurrences of elements in an array

  • Finding the frequency of words in a string

  • Grouping elements of an array by their type

  • Implementing memoization

  • Implementing caches

  • Implementing symbol tables in compilers

In this blog post, we learned about hash map implementation in Ruby and saw several examples of how they can be used in practice. From counting occurrences of elements in an array to grouping elements of an array by their type, hash maps are a versatile data structure that can make our lives as developers much easier.

I hope this article helped give you a deeper understanding of hash maps and how to implement them in Ruby. If you have any questions, comments, or examples of your own, feel free to reach out. I'd love to hear from you!

Remember to share your thoughts, and other implementations in different languages, or simply keep in touch.

Thank you for reading!

Top comments (0)