Published in


Code, Sleep, REPEAT — Hash on Ruby

This blog article is retrieved from a blog assignment originally written on 4/15/2020. In this blog, I explain how I overcame a difficult lab from Flatiron’s Sinatra phase.

Maybe you need some motivation, it will be my pleasure to let you know you’re not alone on this and we all suffer from some imposter syndrome more often than not. If you really just need to know about Ruby Hashes, click on this hyperlink straight to the documentation. Though I think the example code on this blog makes sense, it should be accompanied by the documentation. Once you understand this topic, you can keep practicing Object-Oriented programming and you’ll nail it.


Is that time of the month, the day to do that past-due blog assignment. This month, I would be talking about my experience with Sinatra. Which has been great. Things are getting more exciting and this past week has been smooth, like smooth jazz or like the legend himself.

A few weeks ago, I was making the best out of my Business Intelligence experience (or should I say familiarity with SQL) to work on those lessons. However, I was running too fast and hit myself with a wall called ActiveRecord. It was something new to me. Though is straightforward, we do need to be comfortable with what we have learned so far. And that’s where the CRUD Create, Read, Update, Delete) actions come to play. The CRUD lab consisted of exercises to instantiate and create data on Ruby. For example:

def can_be_created_with_a_hash_of_attributes
# Initialize movie and then and save it
attributes = {
title: "The Sting",
release_date: 1973,
director: "George Roy Hill",
lead: "Paul Newman",
in_theaters: false
movie = Movie.find_by(attributes)

In order to solve the failures, we need to know what we learned in the previous Ruby courses. Assuming we have to develop a CLI program, this should be a simple one. Well, not for me. There was one challenging and difficult exercise. Create the Christmas classic Home Alone if it is in the argument or create the cult classic The Room if there’s no argument.

Sometimes I tried and it was returning an error that was expecting The Room. Which had me shouting out of frustration like this:

Then, I decided to try it another way. Maybe adding that Home Alone argument….

Exactly, I was frustrating. Turns out I needed to do something, repeat. Before realizing the correct code, I needed to review hashes with pair values. The moral of this story is to repeat, then repeat, and then repeat some more. Review the topics, code again, play with for a bit with those codes. This was the correct code that solved the issue:

def can_be_created_in_a_block(m = {title: "Home Alone", release_date: 1990})
movie = Movie.create do |mov|
mov.title = m[:title]
mov.release_date = m[:release_date]

That would also create both Home Alone and the amazing bad movie The Room. Remember fellow newbie, learn the code and keep practicing. Repetition is the key to get the value out of what we’re learning.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store