For C programmers, another way to look at blocks is that it is like passing a function pointer, except that instead of specifying the address of a function that you wrote elsewhere, you just write the text of the function.<br>
<br>
<div><span class="gmail_quote">On 10/14/08, <b class="gmail_sendername">Ron Teitelbaum</b> &lt;<a href="mailto:Ron@usmedrec.com">Ron@usmedrec.com</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi Tony,<br><br>You stumbled on one of the most powerful features of Smalltalk.&nbsp;&nbsp;The Block<br>(See BlockContext).&nbsp;&nbsp;Blocks are a contextual memory space.&nbsp;&nbsp;They can be<br>
passed around and do all sorts of great things that Smalltalk programmers<br>take for granted.<br><br>The basic form is [] this is a no argument, no code block.&nbsp;&nbsp;Pretty boring<br>cause it does nothing.<br><br>A more advanced form is [&#39;hello&#39;]&nbsp;&nbsp;which is a block with a literal string.<br>
Still pretty boring.&nbsp;&nbsp;But at least you can get the string out of the block<br>by&nbsp;&nbsp; aBlock := [&#39;hello&#39;].&nbsp;&nbsp;^aBlock value.<br><br>A bit more advanced: [:arg | &#39;Hello &#39;, arg]&nbsp;&nbsp;has an argument.<br>Now you can do ^aBlock value: &#39;Ron&#39;.<br>
<br>You can have more arguments [:arg1 :arg2 | &#39;Hello &#39;, arg1, &#39; &#39;, arg2].<br><br>Now you can do ^aBlock value: self firstName value: self lastName.<br><br>Even more complicated is:<br>| isLoggedIn |<br><br>
isLoggedIn := true.<br><br>[:arg | &#39;Hello &#39;, arg, &#39; you are &#39;, (isLoggedIn ifTrue: [&#39;&#39;] ifFalse: [&#39;<br>not&#39;]), &#39; logged in&#39;]<br><br>Now you can do ^aBlock value: &#39;Ron&#39;. From anywhere and the block remembers<br>
the context from where it was created.&nbsp;&nbsp;Pretty cool huh.<br><br>The regular select uses a block too:<br><br>self select: [:anItem | anItem isBlue]<br><br>which uses a do that uses a block<br><br>self do: [:anElement |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aBlock value: anElement) ifTrue ...<br>]<br><br>Blocks are certainly a good thing to learn.<br><br>Happy Coding,<br>Ron Teitelbaum<br><br>&gt; -----Original Message-----<br>&gt; From: <a href="mailto:beginners-bounces@lists.squeakfoundation.org">beginners-bounces@lists.squeakfoundation.org</a> [mailto:<a href="mailto:beginners-">beginners-</a><br>
&gt; <a href="mailto:bounces@lists.squeakfoundation.org">bounces@lists.squeakfoundation.org</a>] On Behalf Of Tony Giaccone<br>&gt; Sent: Tuesday, October 14, 2008 2:12 AM<br>&gt; To: <a href="mailto:beginners@lists.squeakfoundation.org">beginners@lists.squeakfoundation.org</a><br>
&gt; Subject: [Newbies] Total newb...<br>&gt;<br>&gt; Ok, so I&#39;m really new to smalltalk. I&#39;ve done a few basic tutorials<br>&gt; and have a simple understanding of the syntax. My pervious programing<br>&gt; experience is mostly java/C with a bit of Objective C in the mix.<br>
&gt;<br>&gt; I&#39;m trying to figure out how to do what seems like a simple thing.<br>&gt;<br>&gt; I have a set, I&#39;d like to find out if an object exists in the set.<br>&gt;<br>&gt; In a general form. Let&#39;s use the a relatively simple case.<br>
&gt;<br>&gt; Assume I have classes Rock Paper and Scissors.<br>&gt;<br>&gt;<br>&gt; validHands := Set new.<br>&gt; validHands add: Rock new; add Paper new; add Scissors&nbsp;&nbsp;new.<br>&gt;<br>&gt; Assume I have a player object which responds to the method<br>
&gt; throwsAHand with an instance of Rock Paper or Scissors.<br>&gt;<br>&gt; how do I craft<br>&gt;<br>&gt; validHands contains: aPlayer throwsAHand<br>&gt;<br>&gt; I know that contains: takes a block, and that this isn&#39;t correctly<br>
&gt; done.. but I&#39;m trying to get the a handle on how to do this.<br>&gt; The intent is to return a boolean, that indicates if the object the<br>&gt; player threw is in the Set of valid objects that can be thrown.<br>
&gt;<br>&gt;<br>&gt; Tony<br>&gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; Beginners mailing list<br>&gt; <a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
&gt; <a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br><br>_______________________________________________<br>Beginners mailing list<br>
<a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>