<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Using Ruby Blocks And Rolling Your Own Iterators</title> <atom:link href="http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/feed/" rel="self" type="application/rss+xml" /><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/</link> <description>For the betterment of the software craft...</description> <lastBuildDate>Mon, 21 Nov 2011 13:57:06 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.2</generator> <item><title>By: Offirmo</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-7170</link> <dc:creator>Offirmo</dc:creator> <pubDate>Fri, 10 Jun 2011 11:27:02 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-7170</guid> <description>Man, you&#039;re good !</description> <content:encoded><![CDATA[<p>Man, you&#8217;re good !</p> ]]></content:encoded> </item> <item><title>By: Ochronus</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-7122</link> <dc:creator>Ochronus</dc:creator> <pubDate>Thu, 05 May 2011 13:10:04 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-7122</guid> <description>Thanks for the post! I&#039;ve also written an article about&lt;a href=&#039;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&#039; rel=&quot;nofollow&quot;&gt;Ruby blocks and closures&lt;/a&gt; with code examples.</description> <content:encoded><![CDATA[<p>Thanks for the post! I&#8217;ve also written an article about<a
href='http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/' rel="nofollow">Ruby blocks and closures</a> with code examples.</p> ]]></content:encoded> </item> <item><title>By: Webs Developer &#187; Closures – A Simple Explanation (Using Ruby)</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-5603</link> <dc:creator>Webs Developer &#187; Closures – A Simple Explanation (Using Ruby)</dc:creator> <pubDate>Tue, 18 May 2010 21:30:28 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-5603</guid> <description>[...] Using Ruby Blocks And Rolling Your Own Iterators [...]</description> <content:encoded><![CDATA[<p>[...] Using Ruby Blocks And Rolling Your Own Iterators [...]</p> ]]></content:encoded> </item> <item><title>By: Functional Programming in Ruby &#124; Yellow™ Lab Blog</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2778</link> <dc:creator>Functional Programming in Ruby &#124; Yellow™ Lab Blog</dc:creator> <pubDate>Tue, 15 Sep 2009 00:25:56 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2778</guid> <description>[...] be assigned to a variable so their reuse is limited. One of the Yellow Lab developers has written more about Ruby blocks on his [...]</description> <content:encoded><![CDATA[<p>[...] be assigned to a variable so their reuse is limited. One of the Yellow Lab developers has written more about Ruby blocks on his [...]</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2644</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Fri, 04 Sep 2009 12:13:13 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2644</guid> <description>The distinction is pretty fine and as I said the semicolon feature seems to be pretty useless, so perhaps my example was a little unclear about this one.
Here is the way I think about it. A block can have arguments, when you call an iterator and give it a block, the iterator will automatically assign values to these arguments. The block also has access to variables in the scope it was defined without having to pass them in that don&#039;t conflict in name with the block arguments (in 1.9). So, if you&#039;re using a coma you would be expecting the iterator to assign some values to the arguments that you pass in.
When you use the semicolon, you&#039;re essentially creating a variable with block scope. It will not be automatically assigned to and it will not have a value set even if it has the same name as a variable in scope when the block was defined. So, you will essentially be shadowing a variable with the same name that is already in scope or creating a whole new variable with block scope.
The only place that I can see where this is useful would be if you coded an iterator that expects a block with a certain number of parameters and you want to pass in more parameters (that you would like to use within the block), you could do this with the semicolon, but you could not do this with the coma.
What this really means is if you want to have a variable with a particular name inside the block which is distinct from the variables that the iterator will automatically assign to (i.e. you really like the name &#039;foobar&#039; but you already have a &#039;foobar&#039; variable in scope when you define the block) then you will be able to write the block pass in whatever variables the block already expects and then pass in your &#039;foobar&#039; variable which will shadow the one already in scope and thereby protect it from being modified within your block.</description> <content:encoded><![CDATA[<p>The distinction is pretty fine and as I said the semicolon feature seems to be pretty useless, so perhaps my example was a little unclear about this one.</p><p>Here is the way I think about it. A block can have arguments, when you call an iterator and give it a block, the iterator will automatically assign values to these arguments. The block also has access to variables in the scope it was defined without having to pass them in that don&#8217;t conflict in name with the block arguments (in 1.9). So, if you&#8217;re using a coma you would be expecting the iterator to assign some values to the arguments that you pass in.</p><p>When you use the semicolon, you&#8217;re essentially creating a variable with block scope. It will not be automatically assigned to and it will not have a value set even if it has the same name as a variable in scope when the block was defined. So, you will essentially be shadowing a variable with the same name that is already in scope or creating a whole new variable with block scope.</p><p>The only place that I can see where this is useful would be if you coded an iterator that expects a block with a certain number of parameters and you want to pass in more parameters (that you would like to use within the block), you could do this with the semicolon, but you could not do this with the coma.</p><p>What this really means is if you want to have a variable with a particular name inside the block which is distinct from the variables that the iterator will automatically assign to (i.e. you really like the name &#8216;foobar&#8217; but you already have a &#8216;foobar&#8217; variable in scope when you define the block) then you will be able to write the block pass in whatever variables the block already expects and then pass in your &#8216;foobar&#8217; variable which will shadow the one already in scope and thereby protect it from being modified within your block.</p> ]]></content:encoded> </item> <item><title>By: dan</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2643</link> <dc:creator>dan</dc:creator> <pubDate>Fri, 04 Sep 2009 11:49:57 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2643</guid> <description>&quot;By passing a variable to the block after the semicolon, we essentially say that we want to have a variable with that name local to the block and unrelated to a variable with the same name outside the block e.g.:&quot;
Isn&#039;t this the standard 1.9 behaviour (with either a comma or a semi-colon)?</description> <content:encoded><![CDATA[<p>&#8220;By passing a variable to the block after the semicolon, we essentially say that we want to have a variable with that name local to the block and unrelated to a variable with the same name outside the block e.g.:&#8221;</p><p>Isn&#8217;t this the standard 1.9 behaviour (with either a comma or a semi-colon)?</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2642</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Fri, 04 Sep 2009 11:43:58 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2642</guid> <description>Oops, yeah you&#039;re of course correct, it should be squares, nice pick-up. Thanks for letting me know, I will update.</description> <content:encoded><![CDATA[<p>Oops, yeah you&#8217;re of course correct, it should be squares, nice pick-up. Thanks for letting me know, I will update.</p> ]]></content:encoded> </item> <item><title>By: dan</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2640</link> <dc:creator>dan</dc:creator> <pubDate>Fri, 04 Sep 2009 11:34:51 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2640</guid> <description>Um.  Squared, not doubled, Right?
If we then call out iterator method with a block that doubles all the values it receives:
puts [2,4,6,8].reverse_iterate { &#124;i&#124; i*i}
the result returned from the method call will be an array with the values of our original     array but reversed and doubled. We can print this out:
64
36
16
4</description> <content:encoded><![CDATA[<p>Um.  Squared, not doubled, Right?</p><p> If we then call out iterator method with a block that doubles all the values it receives:</p><p> puts [2,4,6,8].reverse_iterate { |i| i*i}<br
/> the result returned from the method call will be an array with the values of our original     array but reversed and doubled. We can print this out:</p><p> 64<br
/> 36<br
/> 16<br
/> 4</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2633</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Fri, 04 Sep 2009 00:54:56 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2633</guid> <description>I always get those two mixed up :). It&#039;s usually ok since most people can contextually get what you mean, but it does sometimes make for a confusing few minutes of conversation :).</description> <content:encoded><![CDATA[<p>I always get those two mixed up :). It&#8217;s usually ok since most people can contextually get what you mean, but it does sometimes make for a confusing few minutes of conversation :).</p> ]]></content:encoded> </item> <item><title>By: Mark Wilden</title><link>http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/comment-page-1/#comment-2631</link> <dc:creator>Mark Wilden</dc:creator> <pubDate>Fri, 04 Sep 2009 00:35:51 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1136#comment-2631</guid> <description>&quot;to get this to work correctly we need to wrap the whole thing in braces&quot; should be &quot;parentheses&quot;. But good explanation - I&#039;ll never forget the difference between {} and do/end again.</description> <content:encoded><![CDATA[<p>&#8220;to get this to work correctly we need to wrap the whole thing in braces&#8221; should be &#8220;parentheses&#8221;. But good explanation &#8211; I&#8217;ll never forget the difference between {} and do/end again.</p> ]]></content:encoded> </item> </channel> </rss>
