Ruby’s Initialize Methods

Joshua Lingbloom
4 min readAug 11, 2021

If you’re new to Ruby as I am you can be amazed and frankly overwhelmed at all the cool methods you can use in your code. You can tie whole lengths of code to single objects, or better yet classes. I’ve been learning more and more about different ways to manipulate code, and one of my favorite methods I’ve learned so far is the Initialization method.

Take this code for instance…

The above code is creating three instances of the class “Person”, essentially making three people all with the same names and age keys. As you can see this is quite a bit of code to write for something that is otherwise pretty simple. There are alot of redundancies and you can see the same content being coded multiple times. Lets see how we can reconfigure this code to make it a little easier and nicer to read…

Wow this looks a lot nicer to look at, and there are a few less lines as well! Lets try and break down what’s going on here. Firstly we took all of our information we needed for each instance of person and instead we put it inside of our “initialize()” method. What does this do? Pretty much its a method that gets called every time and instance of the class its nested in is used. So every time we add a new person using the Person.new() we are calling the initialization. We can use this to add our keys and parameters inside the parenthesis.

We can then set the attributes in our “Person” class object with the attributes we are passing down from our initialize method using the “@” method. This is pretty much saying our parameters we are passing down to each object created are the same. So now we can write every instance of person without having to manually tie it the the class object saving so much time! Mike Dane goes even more into this method for reference https://youtu.be/Js2eKsgHrFo.

However there can come some draw back to doing this. Our code will now expect we use a value for each parameter. What if we have some gaps in our data, like a person who doesn't have an age? Well there’s a pretty simple way to get around this by creating “default values”

We can set default values for each attribute by adding an assignment operator to whatever default value you want! Now say if we try and make a person object without a last name we will get an “Unknown” as our last name. Or we can have it return nothing by adding an empty string like we did with age. these are all helpful methods we can fall back on when were working with larger amounts of code.

There are still some things to look out for when setting up your methods like this. Our initialize method is still looking for three different values, “first_name”, “last_name”, followed by “age” respectively. This relies on our person data to come in a specific order. What happens if we get our person data mixed up? For example we try adding a person who’s “age” value come before our “first_name”?

You can probably guess the output we would receive. We’d end up with a person with the “first_name = 26, last_name = Emily, and age = Logger”. Even more problematic, say our code was expecting a string value and got an Integer! This can run all down your code.

This is an issue that I imagine people have ran into in the past and I will have to answer at a later date! Try solving it yourself!Anyways these Methods can help simplify your code and add allot more readability!

sources:

https://youtu.be/Js2eKsgHrFo,

http://ruby-for-beginners.rubymonstas.org/writing_classes/attribute_writers.html

--

--