Home Segments Index Top Previous Next

383: Mainline

You can, of course, insert the expression shown in Segment 382 into a method such that messages based on that method, when sent to Array instances containing numbers, add up the numbers in the array. There is no need for a variable, such as Calories, however; self names the array:

Array method definition • instance 
sumUp 
  | sum | 
  sum := 0. 
  self do: [:element | sum := sum + element]. 
  ^ sum 
Workspace
Transcript show: (Array with: 570 with: 720 with: 640 with: 720) 
                 sumUp printString 
Transcript 
2650