How can I send a block and its argument to a method ? so the method receive the block and the block receives the argument and I run the block in the method iteslf...
Asked
Active
Viewed 624 times
2 Answers
2
Just pass the block and its argument to the method as separate arguments. Then send #value:
to the block to pass the argument to the block. E.g.
methodTaking: aBlock and: anArgument
aBlock value: anArgument.
...

Sean DeNigris
- 6,306
- 1
- 31
- 37
-
how do I pass the argument with the block ? in this way: object methodTaking: aBlock: #value: argument. – Ohad Nov 13 '14 at 02:59
-
-
You were close. "object methodTaking: aBlock and: argument". value: is a message sent to the block in the first statement of the #methodTaking:and: method. Judging by your questions, there are some basic concepts missing here. It will be very difficult to piece then together on SO. IMHO you'd be much better off to start with a Smalltalk intro book like Squeak By Example" – Sean DeNigris Nov 13 '14 at 03:08
-
Either that or pair program a few hours with a smalltalker. You take the keyboard and (s)he talks. – Stephan Eggermont Nov 14 '14 at 09:04
1
For an example have a look at the sort:
method of OrderedCollection
(you'll find the block evaluated finally in SortedCollection>>mergeFirst:middle:last:into:by:
).
Inside a method that accepts a block as parameter, you would evaluate the block, this means call it with parameters and use the result. Not so much try to "access the argument of the block".
You would e.g. send a message with a block as parameter to a collection of colors to sort it by luminance:
colors := OrderedCollection new.
colors addAll: { Color red. Color yellow. Color white. Color black }.
colors sort: [:c1 :c2 | c1 luminance <= c2 luminance].
results in: "an OrderedCollection(Color black Color red Color yellow Color white)"

MartinW
- 4,966
- 2
- 24
- 60
-
hey.. the thing is the argument of my block is a Link of a LinkedList... so when i send the argument to the block, and the block to the method, the method should delete the argument from the list if the block returns true.. this is why I do need to get a way to access it..in order to delete than element from the list I need to access it.. – Ohad Nov 12 '14 at 21:58