<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Ruby Blocks, do or brace?</title>
    <link>http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Ruby Blocks, do or brace?</title>
      <description>&lt;div style='width:240; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid;margin-bottom:30px; margin-right:30px;' class='tease-image'&gt;
&lt;img width='240' height='180' alt='Red Block' src='http://farm1.static.flickr.com/55/132243417_39300a8258_m.jpg'&gt;
&lt;br/&gt;
&lt;a href='http://flickr.com/photos/uwehermann/132243417/'&gt;Red Block&lt;/a&gt;
&lt;br/&gt;&amp;copy;
&lt;a href='http://flickr.com/people/uwehermann'&gt;&lt;/a&gt;
&lt;br/&gt;&lt;a href='http://creativecommons.org/licenses/by-sa/2.0/'&gt;&lt;img src='http://i.creativecommons.org/l/by-sa/2.0/80x15.png' title='used under a Creative Commons Attribution-ShareAlike License' width='80' height='15' border='0'/&gt;&lt;/a&gt;
&lt;/div&gt;
Conventional wisdom in Ruby is to use do/end to delimit blocks which contain more than one line of code, and braces for one-line blocks.  I've always tended to loosely follow this advice.
&lt;p&gt;Thanks to ruby-talk, I just became aware of Jim Weirich's suggestion&lt;/a&gt; to use braces for blocks when the value is being used, and do/end for blocks which are primarily sequences of statements. Jim actually &lt;a href="http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc"&gt;posted this over three years ago&lt;/a&gt;, and Joe O'Brien &lt;a href="http://objo.com/2007/6/28/ruby-style-ruby-do-end-versus"&gt;brought it up more recently.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On the whole, I like this idea and will probably adopt it to tune my use of do/end vs. braces.&lt;/p&gt;



&lt;h2&gt;But don't all blocks return a value?&lt;/h2&gt;
&lt;p&gt;Here's Jim's prescription:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Use { } for blocks that return values&lt;/li&gt;
	&lt;li&gt;Use do / end for blocks that are executed for side effects&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the pedantic minded, it's true that all Ruby blocks actually return values. Every Ruby statement and expression, returns a value. Some folks have used this to object to the suggestion, or at least question it.&lt;/p&gt;
&lt;p&gt;At the risk of putting words in Jim's mouth, I believe that he's really talking here about the use/non-use of the value of the block.  If you &lt;strong&gt;use&lt;/strong&gt; the value of the block then tilt more to using braces.  If the value is &lt;strong&gt;discarded&lt;/strong&gt; then lean towards do/end.&lt;/p&gt;
&lt;h2&gt;Why &lt;strong&gt;I&lt;/strong&gt; Like this idea&lt;/h2&gt;
&lt;p&gt;Visually {x + 7} feels more like ( x + 7) than do; x + 7; end does.&lt;/p&gt;
&lt;h2&gt;Related Things&lt;/h2&gt;
&lt;p&gt;I've always been slightly uncomfortable writing code like this:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;collection.select do | x |
  something
  something_else
end.some_other_method&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Instead I think that:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;collection.select { | x |
	something
	something_else
}.some_other_method&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just &lt;strong&gt;looks&lt;/strong&gt; better to my eye, despite the multi-line block body.&lt;/p&gt;
&lt;p&gt;When I first started writing this, I was thinking that this was an example of Jim's style, but it really isn't.  First of all the value of the block is being used, but not directly in the code we're looking at. It's being used internally by the select method.&lt;/p&gt;
&lt;p&gt;Now I don't think that you should use braces for any block passed to a method like select, even though the value is being used there.&lt;/p&gt;
&lt;p&gt;Second, the value we're using here isn't the value of the block but the value of the select method call.&lt;/p&gt;
&lt;p&gt;That said, I still like using the braces in this case.&lt;/p&gt;
&lt;h2&gt;Another tension&lt;/h2&gt;
&lt;p&gt;Keep in mind that you aren't completely free to substitute braces for do/end.  Sometimes you need to bow to Ruby's precedence rules.  Generally in Ruby if something has both a word and a symbolic representation, e.g. {} vs do/end, || vs or, etc. The symbolic version has higher syntactic precedence than the word version.  This comes up at times, particularly when you are using a DSL in it's 'natural' mode and not using 'unnecessary' parentheses.  For example in Rake:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;description 'pickup up the leaves'
task :rake =&amp;gt; pre_raking_tasks do
  #code to rake the leaves
end&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Works as expected, but:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;description 'pickup up the leaves'
task :rake =&amp;gt; pre_raking_tasks {#code to rake the leaves}&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Won't because it actually &lt;strong&gt;means&lt;/strong&gt;&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;description 'pickup up the leaves'
task :rake =&amp;gt; (pre_raking_tasks {#code to rake the leaves})&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The block is given to the pre_raking_tasks invocation rather than the task invocation.  So you either need to use do/end or parenthesize explicitly:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;description 'pickup up the leaves'
task(:rake =&amp;gt; pre_raking_tasks) {#code to rake the leaves})&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Tue, 02 Oct 2007 07:04:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:96267c47-cfa4-4001-9a87-fa3ad3ea57d1</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace</link>
      <category>ruby</category>
      <category>best_practices</category>
      <category>rubystyle</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/465</trackback:ping>
    </item>
    <item>
      <title>"Ruby Blocks, do or brace?" by FrankLamontagne</title>
      <description>&lt;p&gt;Hey, that&amp;#8217;s a nice idea!&lt;/p&gt;


	&lt;p&gt;I also think that :&lt;/p&gt;


	&lt;p&gt;collection.select { | x |
    something
    something_else
}.some_other_method&lt;/p&gt;


	&lt;p&gt;looks better than the &amp;#8220;do, end&amp;#8221; version.&lt;/p&gt;</description>
      <pubDate>Thu, 11 Oct 2007 19:08:59 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f4f2cd60-0374-4a73-9ce8-05bb776bc545</guid>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace#comment-1316</link>
    </item>
    <item>
      <title>"Ruby Blocks, do or brace?" by James O'Kelly</title>
      <description>&lt;p&gt;I agree with most of this, although returning a value isn&amp;#8217;t what I use to determine braces or do end.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s a multi-line thing for my, but as you say having an end.function() is really ugly, so that is when I break my multi-line rule.&lt;/p&gt;</description>
      <pubDate>Sun, 07 Oct 2007 05:16:07 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:49c3da0f-d644-47e9-85cb-90fd4fcf34de</guid>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace#comment-1273</link>
    </item>
    <item>
      <title>"Ruby Blocks, do or brace?" by Jim Weirich</title>
      <description>&lt;p&gt;The words you put in my mouth were the ones I would have put there myself :)&lt;/p&gt;


	&lt;p&gt;On the &amp;#8220;select{...}.method&amp;#8221; thing, it is true that my rule concerns the use of the value returned by the block, not the value returned by select.  But surprising, most of the time blocks that return values are used with methods that return values of interest.  E.g. I&amp;#8217;m almost never interested in the value returned by :each.  But the value return by select is almost always useful (and a candidate for further method applications).&lt;/p&gt;</description>
      <pubDate>Tue, 02 Oct 2007 11:40:52 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:272e271b-b171-4d33-9772-24c7dbf9e51d</guid>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace#comment-1222</link>
    </item>
  </channel>
</rss>
