In this section we will learn what the initialize method is and how to use it in Ruby.
What is initialize method in Ruby?
The initialize method is a method that will be called automatically when we create an object from a class and so it is usually used in order to create and initialize instance variables of objects.
Note that by default every class has an empty initialize method in them (This is added automatically behind the scene when our program is running).
But we can add our own initialize method to a class and set its body the way we want to.
Note: for those of you who are coming from Java, C#, C++, or Python, initialize method acts the same as constructor methods in those languages.
How to create the initialize method in Ruby?
The initialize method is like other methods we’ve seen and created so far with the exception that its name is already known and it is `initialize`. Other than that, the process of creating an initialize method in a class is exactly the same as creating other methods.
For example:
class ClassName def initialize (param1, param2, param3) @variable_instance = param1 @variable_instance = param2 @variable_instance = param3 end end
The initialize method can have one or more parameters depending on the requirements of the class. And within its body, we can define as many instance variable as needed for an object.
Example: creating initialize method in Ruby class
class Person attr_accessor :first_name, :last_name, :id def initialize(first_name, last_name, id) @first_name = first_name @last_name = last_name @id = id end end prs = Person.new("John","Doe",500) puts prs.first_name, prs.last_name, prs.id
Output:
John Doe 500
How does initialize method work in Ruby?
When we don’t have an initialize method in our class (didn’t create one explicitly) then the class will get an empty one by default and behind the scene.
But when we create our own one specially with parameters set for it, then we can’t just call the `new` method like the way we called it when there wasn’t any initialize method in a class.
The `new` method calls the initialize method on behalf of us and it needs to pass arguments to the initialize method if it needs one! So it’s our responsibility to pass any necessary argument that the target initialize method might need, to the `new` method.
Now, because the initialize method has three parameters, we need to pass three arguments to the `new` method and they will be passed to the parameters of the initialize method then.
Ruby initialize method and default arguments
The initialize method is a method just like any other method we might have in a class. This means we can set default arguments for the initialize parameters as well.
In that case, the user can skip over the parameters with optional arguments when she’s initializing an object from the target class.
Example: using default arguments in initialize method
class Person attr_accessor :first_name, :last_name, :id, :company def initialize(first_name, last_name, id, company = "Google") @first_name = first_name @last_name = last_name @id = id @company = company end end prs = Person.new("John","Doe",500) puts prs.first_name, prs.last_name, prs.id, prs.company
Output:
John Doe 500 Google
Here we didn’t pass an argument for the `company` parameter when creating an object from the `Person` class and so because this parameter has a default value, it was used within the body of the `initialize` method then.