Home Segments Index Top Previous Next

121: Mainline

You define the convertToCaloriesWith: method in basically the same way that you learned to define the fatToCalories method in Chapter 4. The only difference is the addition of a parameter:

Integer method definition • instance 
convertToCaloriesWith: caloriesPerGram 
  ^ self * caloriesPerGram 

The following diagram shows what each part of the method definition does:

 *-- The method selector 
 | 
 |                      *-- The method's parameter 
 |                      | 
 v                      v 
---------------------- --------------- 
convertToCaloriesWith: caloriesPerGram 
    ^ self * caloriesPerGram 
    ^ ---------------------- 
    |  ^ 
    |  | 
    |  *-- An expression to be evaluated 
    | 
    *-- Answer marker