Our Fruit class from the previous chapter had two instance variables, one to describe the kind of fruit and another to describe its condition. It was only after writing a custom
method for the class that we realized it didn't make sense for a piece of fruit to lack those characteristics. Fortunately, ruby provides a way to ensure that instance variables always get initialized.
- inspect
The
- initialize
method
- initialize
Whenever Ruby creates a new object, it looks for a method named
and executes it. So one simple thing we can do is use an
- initialize
method to put default values into all the instance variables, so the
- initialize
method will have something to say.
- inspect
- ruby> **class Fruit**
- |**def initialize**
- |**@kind = "apple"**
- |**@condition = "ripe"**
- |**end**
- | **end**
- **nil**
- ruby> **f4 = Fruit.new**
- **"a ripe apple"**
Changing assumptions to requirements
There will be times when a default value doesn't make a lot of sense. Is there such a thing as a default kind of fruit? It may be preferable to require that each piece of fruit have its kind specified at the time of its creation. To do this, we would add a formal argument to the
method. For reasons we won't get into here, arguments you supply to
- initialize
are actually delivered to
- new
.
- initialize
- ruby> **class Fruit**
- |**def initialize( k )**
- |**@kind = k**
- |**@condition = "ripe"**
- |**end**
- | **end**
- **nil**
- ruby> **f5 = Fruit.new "mango"**
- **"a ripe mango"**
- ruby> **f6 = Fruit.new**
- **ERR: (eval):1:in `initialize': wrong # of arguments(0 for 1)**
Flexible initialization
Above we see that once an argument is associated with the
method, it can't be left off without generating an error. If we want to be more considerate, we can use the argument if it is given, or fall back to default values otherwise.
- initialize
- ruby> **class Fruit**
- |**def initialize( k="apple" )**
- |**@kind = k**
- |**@condition = "ripe"**
- |**end**
- | **end**
- **nil**
- ruby> **f5 = Fruit.new "mango"**
- **"a ripe mango"**
- ruby> **f6 = Fruit.new**
- **"a ripe apple"**
You can use default argument values for any method, not just
. The argument list must be arranged so that those with default values come last.
- initialize
Sometimes it is useful to provide several ways to initialize an object. Although it is outside the scope of this tutorial, ruby supports object reflection and variable-length argument lists, which together effectively allow method overloading.