<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Moserei &#187; block</title>
	<atom:link href="http://moserei.de/index.php/tag/block/feed" rel="self" type="application/rss+xml" />
	<link>http://moserei.de</link>
	<description></description>
	<lastBuildDate>Tue, 06 Jul 2010 16:18:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Difference between do-end and {} blocks</title>
		<link>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks</link>
		<comments>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks#comments</comments>
		<pubDate>Sun, 07 Feb 2010 12:44:41 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[block precedence]]></category>
		<category><![CDATA[curly]]></category>
		<category><![CDATA[do end]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=190</guid>
		<description><![CDATA[There is a important difference between the two block syntaxes in Ruby: Their precedence.
Consider following code:

def method1&#40;*args&#41;
  puts &#34;method1 got a block&#34; if block_given?
end
&#160;
def method2&#40;*args&#41;
  puts &#34;method2 got a block&#34; if block_given?
end
&#160;
method1 method2 do
end
&#160;
method1 method2 &#123;
&#125;

You would expect both method calls to produce the same result, wouldn&#8217;t you?
Output:

method1 got a block
method2 got a [...]]]></description>
			<content:encoded><![CDATA[<p>There is a important difference between the two block syntaxes in Ruby: Their precedence.<br />
Consider following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> method1<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;method1 got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> method2<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;method2 got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
method1 method2 <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
method1 method2 <span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>You would expect both method calls to produce the same result, wouldn&#8217;t you?<br />
Output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">method1 got a block
method2 got a block</pre></div></div>

<p>Obviously the do-end block is passed to the first method in the expression while the {} block is passed to the method called directly before it.<br />
Another example shows that the assignment does not count as a method call here:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#[...]</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> bar=<span style="color:#006600; font-weight:bold;">&#40;</span>o<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Foo#bar= got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
foo = Foo.<span style="color:#9900CC;">new</span>
&nbsp;
foo.<span style="color:#9900CC;">bar</span> = method1 method2 <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#9966CC; font-weight:bold;">end</span> 
<span style="color:#008000; font-style:italic;">#method1 got a block</span>
&nbsp;
foo.<span style="color:#9900CC;">bar</span> = method1 method2 <span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span> 
<span style="color:#008000; font-style:italic;">#method2 got a block</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
