Jacob Short
3 min readOct 17, 2020

Arguments, should you have them, and when. In Ruby.

Passing in a hash as an argument… passing key-value pairs as an argument. Setting the default argument to a hash. Using hashes as default arguments?

I recently started my journey at Flatiron coding boot camp on the fifth of October and I have loved every moment of it. That’s when the trouble with arguments began and I had to take action. No one should see the vast treasure trove of information that is the internet, only to come up short in search of an answer so quickly after starting their coding journey.

ActiveReader is a great tool we were just introduced to and it makes our lives infinitely easier. That’s why the below picture caused me much grief.

This is all good and well except what in the world is a block?

After some quick reading, I was confused. One person states it’s everything in between ‘do’ and ‘end’ statements. Another states that it’s similar to an anonymous function in JS in that it’s called once with no name and never seen again. Essentially, it’s just everything in between the ‘do’ and ‘end’ statements, or in the curly brackets of a one-liner, for a method. Then why call this method ‘can_be_created_in_a_block’? I would like to file a complaint to the people that name things.

Nonetheless, that’s not the moral of the story. Blocks, as delightful as they are, were not leading me closer to an answer.

Default arguments are my next step.

Simple enough. I understand default arguments well and good.

Mhmm… after much searching and coming up short I began to experiment with what I knew and what I could find. When I finally got my test to work, I found it looked little like the examples the Google was giving me.

This is the solution I came up with:

I find this slightly more readable:

Now up until this point, I had never seen such. The examples online were not as explicit and frankly confusing. Not that I am hard to confuse but maybe someone else out there wants it stupid simple as well.

So we can set a hash as a default argument and set that default hash’s keys equal to the key names in the class instance we are creating so that the key-value pairs in our default argument only get used when we don’t pass in an argument. You need to use the same key names as your hash or class. You need to pass in a hash as an argument with those key names. You cannot pass in strings or integers or anything besides a hash with the correct key names.

I find this a nontrivial issue since as boot camp students our time is limited when we are learning so much so fast. Since there was not a resource I found acceptable to explain this, I decided to make one myself. I hope this blog post will find its way to another stalwart boot camp student in the future.

Jacob Short
Jacob Short

Written by Jacob Short

Software, skateboards, and seeing the world.

No responses yet