Use the caret ^
to return a value from a method
Number >> squared
"Answer the receiver multipled by itself."
^ self * self
A method with no caret ^
returns self
Game >> initializePlayers
self players
at: 'tileAction'
put: ...
is equivalent to
Game >> initializePlayers
self players
at: 'tileAction'
put: ...
^ self "<-- optional"
Blocks return the value of their last expression
[ :x |
x + 33.
x + 2 ] value: 5
> 7
The caret ^
in a block has a special meaning...
A caret ^
in a block quits the enclosing method
Integer>>factorial
"Answer the factorial of the receiver."
self = 0 ifTrue: [ ^ 1 ].
self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
self error: 'Not valid for negative integers'
^
) from a block, the method defining the block is terminated^
always terminates the methodself
by default/