How to Create an Initializer Method in a Ruby Class
One thing you may find handy in Ruby development is to create an `initializer` method. If you're wondering **what is an initializer method?**, it is simply a method called `initialize` that will run every time when you create an instance of your class. In this method, you can give values to your variables, call other methods and do just about anything that you think should happen when a new instance of that class is created.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

One thing you may find handy in Ruby development is to create an initializer method. If you're wondering what is an initializer method?, it is simply a method called initialize that will run every time when you create an instance of your class. In this method, you can give values to your variables, call other methods and do just about anything that you think should happen when a new instance of that class is created.

If you're coming from other languages this initializer method is similar to a constructor.

Adding an initializer to a Ruby Class

Let's update our ApiConnector class to utilize an initializer

This update class will look something like this:

class ApiConnector
  def initialize(title, description, url)
    @title = title
    @description = description
    @url = url
  end
end

For now, I'm creating an instance variable for each of my parameters so that I can use these variables in other parts of the application as well.

devCamp Note: I removed the attr_accessor method since the new initialize method will take care of this for us. If you need the ability to call the data elements outside of the class then you would still need to have the attr_accessor call in place.

To test if the initialize method is working, I'm going to create another method in the class that prints these values out.

def testing_initializer
  puts @title 
  puts @description
  puts @url
end

Next, I'll instantiate the class:

api = ApiConnector.new("My title", "My cool description", "google.com")

Since the initialize methods take three parameters, these values have to be passed while creating the class instance or the program will throw an error.

Finally, to test the initialize method, I call:

api.testing_initializer

If I execute this file on the terminal, my output will have all the three values printed out.

large

Working with optional values

Now, what happens when we want to make one of these values optional? For example, what if we want to give a default value to the URL. To do that, We can update our initialize method with the following syntax

def initialize (title, description, url = "google.com")

Now our program will have the same output even if we don't pass the url value while creating a new instance of the class. In this case, the output is unchanged when you have the following code:

api = ApiConnector.new("My title", "My cool description")

So, this is how you can define optional arguments in an initialize method.

How to use named arguments

Though this looks simple, passing arguments can get complex in real-world Ruby applications because some methods may take a large number of arguments. In such cases it becomes difficult to know the order of arguments and what values to assign to them.

To avoid this confusion, I like to utilize named arguments, like this:

class ApiConnector
  def initialize (title:, description:, url: "google.com")
    @title = title
    @description = description
    @url = url
  end

  def testing_initializer
    puts @title 
    puts @description
    puts @url
  end
end

api = ApiConnector.new(title: "My title", description: "My cool description")
api.testing_initializer

So, you can enter the arguments without having to look at the order in the initialize method. Also you even change the order of the arguments without causing an error. So instantiating our class like this:

api = ApiConnector.new(description: "My cool description", title: "My title")

Would not change the behavior of the system. If we weren't using named arguments if we changed the order of the arguments like this the instance variables would end up being assigned to the opposite values.

Overriding Default Values

So what happens if we want to override a default value?

If we update our instantiation call like this:

api = ApiConnector.new(title: "My title", description: "My cool description", url: "yahoo.com")

It will override our default value of google.com.