Question 2-3

Say you want to add a unary instance method into Smalltalk's built-in Integer class called abs, which returns the absolute value of the Integer instance to which the method is applied. Write the definition of this method.

Solution

abs
    self >= 0 ifTrue: [ ^ self ] ifFalse: [ ^ 0 - self ]

Back to Review for Quiz 2