In the last few lessons,
we wrote code like this:
This works, but what would be nicer, is
when we are creating the instance
we can give it some values.
For example, we should be able to say
to the Cofeemaker to make a hot coffee.
Right now, what we are doing
is we are asking Coffeemaker to make
a coffee and then we are asking
the instance to be hot.
We should be able to do this:
Ruby has a special method called initialize
.
The method initialize
is called when
a class creates an instance.
This initialize
method is in all the
classes sitting at the top doing nothing.
In reality, our CoffeeMaker
looks like this.
When we are saying Coffeemaker.new("hot")
,
we are passing the argument hot to the initialize method.
So far, we have not been using it.
But now let's use it: