I am using pharo
. I have a Car
class which has a speed
variable and the class side method withSpeed: aSpeed
which sets speed := aSpeed.
I am trying the following test in the playground.
car := Car withSpeed: 20.
cars := OrderedCollection new.
cars add: car.
Now, I have a car
inside cars
collection.
I want to get the car which has speed = 20
I am trying the following code, but it gives me error:
result := cars select: [ :each | each withSpeed: 20. ].
Any idea what is going wrong?
Given that your Car
has an accessor for speed
, you simply can do
result := cars select: [ :each | each speed = 20. ].
That gets you all cars that have the speed 20
. If you only want one, you should use detect:
myCar := cars detect: [ :each | each speed = 20. ].
Accessors for instance variables (here speed
) typically look like
Car>>speed
^ speed
and
Car>>speed: anObject
speed := anObject.