Last updated
Last updated
What is the #initialize
method?
How does it work?
What do we use it for?
We've already seen new instances of classes being created with the #new
method. For example:
The code above creates a new instance of the Dog
class and sets that object equal to a variable, snoopy
. If we want to give our dog a breed, we have to use the following code:
However, most dogs are born with a breed, not assigned a breed afterwards. How can we model the behavior of dogs being born with a breed in our Dog
class? If only there was a way for us to assign an individual dog a breed automatically upon creation, or instantiation.
Lucky for us, there is! It's called the #initialize
method.
#initialize
MethodWe already know that any Ruby class can produce new instances of itself, via the <Class Name>.new
method, whether or not that class has an #initialize
method. However, if we want each instance of our class to be created with certain attributes, we must define an #initialize
method. An #initialize
method is a method that is called automatically whenever #new
is used.
Let's define an #initialize
method that takes in an argument of a dog's breed and sets a @breed
variable equal to that argument. In other words, let's define our #initialize
method to contain the functionality of the #breed=
method, so that a dog instance will get a breed assigned to it right away when it is created, without us having to explicitly use the #breed=
method.
#initialize
methodNow, we can call #new
like this:
When #new
is called with an argument, it will pass that argument (or arguments) to the #initialize
method and invoke that method. The code in #initialize
will then run, using any arguments from #new
.
The initialize method is what's called a callback method, because it is automatically invoked every time the #new
method is used to create a new instance of the class.
You can also think of the initialize method as a constructor method. A constructor method is invoked upon the creation of an instance of a class and used to help define the instance of that class.
So, because of how we defined our initialize method, every time you type Dog.new("some breed")
, a new dog instance is created that has a breed of "some breed" (i.e. whatever string you give the #new
method).
View on Learn.co and start learning to code for free.