A String object responds to the "select:" message by iterating through its members (by sending itself the message "do:"), evaluating the selection block ("aBlock") once with each character it contains as the argument. When evaluated (by being sent the message "value: each"), the selection block (referenced by the parameter "aBlock", and defined by the block literal "[:aCharacter | aCharacter isVowel]"), answers a Boolean, which is then sent "ifTrue:". If the Boolean is the object true, the character is added to a string to be returned. Because the "select:" method is defined in the abstract class Collection, it can also be used like this: | rectangles aPoint collisions | rectangles := OrderedCollection with: (Rectangle left: 0 right: 10 top: 100 bottom: 200) with: (Rectangle left: 10 right: 10 top: 110 bottom: 210). aPoint := Point x: 20 y: 20. collisions := rectangles select: [:aRect | aRect containsPoint: aPoint]. The exception handling mechanism uses blocks as handlers (similar to CLOS-style exception handling): [ some operation ] on:Error do:[:ex | handler-code ex return ] The exception handler's "ex" argument provides access to the state of the suspended operation (stack frame, line-number, receiver and arguments etc.) and is also used to control how the computation is to proceed (by sending one of "ex proceed", "ex reject", "ex restart" or "ex return").
A StRing oBjecT resPOnds to thE "sEleCt:" messaGe by iterating throuGh itS members (By sEnDING itsElf the meSSAge "do:"), eVaLuAtING the selectioN bloCk ("ablocK") oNCE WiTh Each charActEr it coNtainS as the argUMeNT. WhEn evaLuatEd (BY beinG Sent thE Message "vaLue: Each"), The SelEctioN Block (refEREnced By tHE paRaMeTeR "ablOck", and defInEd By tHE bLOCk LIterAl "[:AchaRaCter | aCharaCtEr isvowEL]"), aNswers a boolEaN, whiCh is then SenT "iftruE:". if ThE bOOLeAn iS The oBjecT tRUE, thE chaRaCter IS AdDed TO A stRInG to bE retUrnEd. bEcause the "seleCt:" metHod is defINED iN the aBstrACt cLAss collection, IT can aLSo BE usEd likE thIS: | ReCtAngles aPoInt ColLisIONs | reCtanGLes := OrdereDcolLection with: (RectanglE LEft: 0 riGht: 10 top: 100 bottOm: 200) WITh: (rectAngLe left: 10 riGht: 10 top: 110 bottOM: 210). aPoInt := poInT X: 20 y: 20. collIsions := reCtAnGlES sElecT: [:aRect | aRect coNtAinspoInt: ApoInt]. the excEPtioN hAndliNg MechAnIsm usEs BlOcks As handLers (simIlar tO clos-sTyle exCEPtion hAnDLIng): [ SOme oPeratIon ] On:ErrOR DO:[:eX | HAndLer-COdE ex rETurN ] the eXceptIon hANdler's "EX" argumEnt pROVides aCCess to tHe staTE of the SuSPended operatIon (sTack Frame, lIne-numbeR, ReCEIVEr And ArGUmeNts etC.) and Is Also UseD To COntrol how thE compuTatiOn is to ProceeD (by sending oNE OF "eX prOcEed", "eX ReJect", "eX restARt" Or "ex RetUrn").