We add 2 to a set
Set new add: 2
> 2
We get 2 and not the set!
Set>>add: newObject
"Include newObject as one of the receiver's elements, but only if not already present. Answer newObject."
[...]
^ newObject
add:
returns its argument, not the receiverSet new add: 2
> 2
To get the collection back, we can use a temporary variable
| s |
s := Set new.
s add: 2.
s
Object >> yourself
^ self
Set new
add: 2;
yourself
> aSet
add:
and yourself
are sent to the new Set
yourself
Set class >> with: anObject
"Answer an instance of me containing anObject."
| instance |
instance := self new.
instance add: anObject.
^ instance
is equivalent to
Set class >> with: anObject
"Answer an instance of me containing anObject."
^ self new
add: anObject;
yourself
Using yourself
makes sure the code returns the new instance
;
and yourself
often go together/