Here is a Fruit class. Each fruit has a name and a number of calories.
class Fruit
def initialize(fruit_name, calories)
@fruit_name = fruit_name
@calories = calories
end
end
Put this in a fruit.rb
file in some folder.
Do these exercises one at a time. Show your entire fruit.rb
class each time, and show your console output with a couple of examples of the new piece you add in each step.
- Change the initializer of
Fruit
so that if you don't provide acalories
argument, we default to 100 calories. - Enhance the
Fruit
class so that you can get the name of a Fruit instance. - Enhance the
Fruit
class so that you can get the number of calories of a Fruit instance. - Enhance the
Fruit
class so that you can set the name of a Fruit instance to another name. - Create a new, empty class called
FruitBasket
in the same file. - Enhance the
FruitBasket
class so that you can initialize it with an array ofFruit
s. - Enhance the
FruitBasket
class so that you can get the number of items in theFruitBasket
. - Enhance the
FruitBasket
class so that you can get the total number of calories of all the fruits in the basket.