0

I want to create a method that gets a block as an argument, and the block gets a parameter as well. If the block returns true it should do something ( for example return 1), and if it returns false it should do something else.

this is what I did.. but I am getting syntax error on the ifTrue... is this the way I should get as a parameter a block that receives an argument?

Mymethod: Block

Block value: 'argument'
ifTrue: [ ^1]. 
ifFalse: [^2].

and the call to the method :

object := myClass new.
argument :=1
boolValue := object Mymethod : [:argument | argument ==1 ]
David Buck
  • 2,847
  • 15
  • 16
Ohad
  • 1,563
  • 2
  • 20
  • 44

1 Answers1

5

the way you wrote it means that #value:ifTrue: message to the Block, and then you are sending #ifFalse: message to nothing (which is not possible at all. If you want to do it in one line, you should use parenthesis:

(Block value: 'argument')
  ifTrue: [ ^1]
  ifFalse: [^2]

Also in smalltalk it's a convention to name variables with uncapitalized, like block or aBlock

Uko
  • 13,134
  • 6
  • 58
  • 106
  • are there actullu people who work with this langauge ? – Ohad Nov 12 '14 at 13:31
  • 2
    @Shiran, again, I'm working with [tag:pharo], which is a fork of [tag:squeak]. You can read about the success stories here: http://pharo.org/success – Uko Nov 12 '14 at 13:33
  • nice .. not to offend anyone.. it just feels so unnetural comparing to java or c++ . – Ohad Nov 12 '14 at 13:37
  • @Shiran it's ok. Speaking with metaphors: _C++_ is like writing a scenario for CPU and telling it what to do in this or that case. _Smalltalk_ is about modeling the community of objects that **talk** to each other and can solve some problems together. It sounds strange, but the model that you have in mind makes big impact. – Uko Nov 12 '14 at 13:44
  • ya I am getting what u say...just a quick question..I was just running this simple program with the block [:x | x=1] (after doing x:=1) and it returned the value for False instead for True.. I was trying to use this operator "==".. but still got 2 instead of 1 .. any idea why ? – Ohad Nov 12 '14 at 13:50
  • 1
    @Shiran no, I need more information, but if you want to discuss more, you can contact pharo community by writing an email to pharo-users mailing list or using IRC. Contacts can be found here: http://pharo.org/community – Uko Nov 12 '14 at 13:58
  • will defintely check it out.. in the meanehile I might just post a new question.. thanx – Ohad Nov 12 '14 at 14:03
  • I got it rigth.. what helped was removing the value:'argument' in the method.. so even if the block gets arguments I should not declare it.. – Ohad Nov 12 '14 at 14:18
  • the squeak-dev mailinglist or beginners (http://lists.squeakfoundation.org/mailman/listinfo/beginners) is a good place to get to know Squeak and Smalltalk too – Tobias Nov 12 '14 at 17:56