DEV Community

Brittany
Brittany

Posted on

 

Day 19 of #100daysofCode - Hashes 101

During my project review, I was asked to iterate over a hash and sanitize the params inside. I am used to iterating over arrays and had forgotten how to iterate over a hash. Along came GOOGLE. I quickly googled, how do you iterate over a hash?

I was only allowed to use the Ruby docs and found this Ruby-Doc

I realized iterating over a hash is relatively similar to an array, so I attempted to use a .map method. It did not take long for me to realize that using the the .map method on a hash returns an array. WHO KNEW? So, I quickly realized that it was necessary to use the .each method. The docs revealed that .each on a hash should be used like this:

h.each {|key, value| puts "#{key} is #{value}" }
Enter fullscreen mode Exit fullscreen mode

Pretty simple. I took this and created a santize_hash in my helpers and copied the code below.

     def sanitize_hash(h)
       h.each {|key, value| [key] = sanitize(value)}
     end
Enter fullscreen mode Exit fullscreen mode

However, this method was not going to work just yet. I had to create an empty hash and place the keys and values into that hash. I had to think back to the beginning, when I first started studying hashes and arrays.

Hashes are essentially dictionaries. They are similar to arrays except they use objects as an index instead of numbers.

There are two ways to create a hash:

hash = Hash.new 
Enter fullscreen mode Exit fullscreen mode

OR

hash = {}
Enter fullscreen mode Exit fullscreen mode

In order to complete my sanitize_hash method I adding an empty hash before iterating. Upon iteration, I added the key to the empty hash with the sanitized value, like this:

     def sanitize_hash(h)
       hash = {}
       h.each {|key, value| hash[key] = sanitize(value)}
       hash
     end
Enter fullscreen mode Exit fullscreen mode

I have discovered how important it is to have an understanding of hashes and plan to expand my knowledge on hashes in the near future.

JUNETEENTH

Song of the day

Top comments (0)

The AI Brief

AI generated git commit messages

Minimize the struggle of remembering what you just coded. AI-generated commits make it easier to manage projects and keep track of changes. The Nutlope/aicommits project demonstrates how AI can improve commit messages.

I open sourced an AI that creates any UI in seconds

Make AI-generated user interfaces a breeze. This open-source project harnesses the power of generative AI technologies like chatGPT to create versatile, quick, and intuitive UI components.

Use AI to commit like a PRO in 1 second

Upgrade your commit message game with AI. Boost your productivity by using ChatGPT to generate commit messages and avoid context switching. OpenCommit is an open-source library that helps you achieve this easily.

Build your own ChatGPT starter kit

Train AI models on custom data for improved domain-specific knowledge. Combine the power of WebView technologies and this starter kit to train your ChatGPT model on specific websites, allowing for better-optimized outcomes.