<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Gray Soft / Rubies in the Rough / A Stylish Critique</title>
  <id>tag:graysoftinc.com,2014-03-20:/posts/111</id>
  <updated>2014-04-27T16:34:55Z</updated>
  <link rel="self" href="http://graysoftinc.com/rubies-in-the-rough/a-stylish-critique/feed.xml"/>
  <link rel="alternate" href="http://graysoftinc.com/rubies-in-the-rough/a-stylish-critique"/>
  <author>
    <name>James Edward Gray II</name>
  </author>
  <entry>
    <title>The 1st Comment on "A Stylish Critique"</title>
    <link rel="alternate" href="http://graysoftinc.com/rubies-in-the-rough/a-stylish-critique#comment_516"/>
    <id>tag:graysoftinc.com,2012-04-02:/comments/516</id>
    <updated>2014-04-27T16:34:55Z</updated>
    <summary>Great article, really like the wisdom in this:

&amp;gt; If you are just cargo culting some rules, I would recommend actively breaking them for a time. You&amp;#39;ll figure out pretty quick if they are important or not. If they are, you&amp;#39;ll have the reason. If...</summary>
    <content type="html">&lt;p&gt;Great article, really like the wisdom in this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are just cargo culting some rules, I would recommend actively breaking them for a time. You'll figure out pretty quick if they are important or not. If they are, you'll have the reason. If they aren't, you'll be free of a useless constraint.&lt;/p&gt;
&lt;/blockquote&gt;</content>
    <author>
      <name>Adam Weller</name>
    </author>
  </entry>
  <entry>
    <title>A Stylish Critique</title>
    <link rel="alternate" href="http://graysoftinc.com/rubies-in-the-rough/a-stylish-critique"/>
    <id>tag:graysoftinc.com,2012-04-01:/posts/111</id>
    <updated>2014-04-27T16:34:55Z</updated>
    <summary>This article takes a detailed look at very style guides for the Ruby language and attempts to divine what we can learn from them, both good and bad.</summary>
    <content type="html">&lt;p&gt;&lt;em&gt;Before getting started, I feel compelled to point out that my dictionary defines a critique as "a detailed analysis and assessment of something."  It seems like we often assume the worst of that word, but that's not how I intend it here.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Ruby community seems to be talking about style guides lately.  So let's talk about them.&lt;/p&gt;

&lt;p&gt;The fact is that you will have many choices if you go looking for style guides for our favorite language.  You can pick from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.zenspider.com/Languages/Ruby/QuickRef.html#general-tips"&gt;The old stuff&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.caliban.org/ruby/rubyguide.shtml#style"&gt;Google's way&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/styleguide/ruby"&gt;GitHub's popular guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Guides from respected Rubyists, like &lt;a href="https://github.com/dkubb/styleguide/blob/master/RUBY-STYLE"&gt;Dan Kubb&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;One of the &lt;a href="https://github.com/chneukirchen/styleguide/network"&gt;many&lt;/a&gt; &lt;a href="https://github.com/bbatsov/ruby-style-guide/network"&gt;forks&lt;/a&gt; of the popular guides on GitHub&lt;/li&gt;
&lt;li&gt;And many &lt;a href="http://www.google.com/search?rls=en&amp;amp;q=ruby+style+guide&amp;amp;ie=UTF-8&amp;amp;oe=UTF-8"&gt;more&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It's obvious that Rubyists care about this topic.  Let's see what's out there and consider what really is and is not useful from these guides.&lt;/p&gt;

&lt;h4&gt;What is a style guide really, and why do we even have them?&lt;/h4&gt;

&lt;p&gt;Defining style guides is surprisingly tough.  I suspect they started out as formatting rules for code, but they have evolved pretty far beyond that now.&lt;/p&gt;

&lt;p&gt;Most guides include general conventions that the author feels are important when writing the language in question.  This can go all the way down to how to use certain constructs, opinions on what the author considers idiomatic, and syntax to outright avoid.&lt;/p&gt;

&lt;p&gt;To sum it up, a style guide is often a "How I Write Language X" opinion piece from the author.&lt;/p&gt;

&lt;p&gt;Style guides do exist for pretty good reasons.  First, most programmers put some effort into making sure that their code looks a certain way.  Generally this is to make it easier to read.  However, you may not see the code as intended if you are using a different indent size or tabs instead of the spaces they intended.&lt;/p&gt;

&lt;p&gt;With teams, these conventions become even more important.  Teams are trying to work in concert, so they need to be able to understand code not written by them.  A set of conventions the team follows can really help this along.&lt;/p&gt;

&lt;p&gt;And when you think about it, who isn't on a team these days?  Aren't open source developers a team?  The entire Ruby community might even be a team with the way we interact online.&lt;/p&gt;

&lt;p&gt;It seems like this is a topic we should all care about, at least a little.&lt;/p&gt;

&lt;h4&gt;The Rules for the Rules&lt;/h4&gt;

&lt;p&gt;Even though we have decided this topic is important, we must never forget how careful we have to be when imposing rules on others.  The goal is to make communication between us easier, not to restrict creativity.&lt;/p&gt;

&lt;p&gt;In my experience, one rule seems to hold sway over all other rules in life whether those rules are for parenting, laws, or programming style guides:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use the fewest amount of rules you can possibly get away with.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If there are more rules than I can easily internalize, the entire point is defeated.  Odds are that the authors don't even consistently follow their own long lists.&lt;/p&gt;

&lt;p&gt;Now, it is worth noting that some list size variation make sense.  For example, GitHub needs to keep their rules minimal for the reasons I have just mentioned.  However, &lt;a href="http://rubyrogues.com/047-rr-coding-disciplines/"&gt;Dan Kubb talked to us on the Ruby Rogues&lt;/a&gt; about how he uses his style guide to try and restrict himself into being more aware as a programmer.  It makes sense that he would have more rules for that.&lt;/p&gt;

&lt;p&gt;That said, Dan's list is still probably a little long, in my opinion.  It's just too much.  Some of his rules actually encourage me to tune out what he's saying, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid bugs.&lt;/li&gt;
&lt;li&gt;Use common sense.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Again, if you lose your audience, the whole point is lost.&lt;/p&gt;

&lt;p&gt;The classic book that describes itself as a style guide, &lt;a href="http://www.pearsonhighered.com/educator/product/Smalltalk-Best-Practice-Patterns/9780134769042.page"&gt;&lt;em&gt;Smalltalk Best Practice Patterns&lt;/em&gt;&lt;/a&gt;, includes ten "Formatting" patterns.  That strikes me as a pretty common sense upper limit.&lt;/p&gt;

&lt;p&gt;Another rule that's key is one that most parents know:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have a good reason for the rule.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your kid hits you with, "But why?," and you have to counter with, "Because I said so!," you lose.  Rules need to be important.  Important things have a reason for their existence.  The reason needs to be a good one too.&lt;/p&gt;

&lt;p&gt;If you are just cargo culting some rules, I would recommend actively breaking them for a time.  You'll figure out pretty quick if they are important or not.  If they are, you'll have the reason.  If they aren't, you'll be free of a useless constraint.&lt;/p&gt;

&lt;p&gt;For example, Ryan Davis's old rules say, "Use parens to disambiguate, otherwise avoid them."  Ultimately, that leads to the &lt;a href="http://heartmindcode.com/2013/09/05/parentheses-in-ruby/"&gt;"Seattle Style" of method definitions&lt;/a&gt;.  GitHub's newer rules say, "Use &lt;code&gt;def&lt;/code&gt; with parentheses when there are arguments."  Both sides will tell you that it reads better.&lt;/p&gt;

&lt;p&gt;I personally find that my eye seems to drift more without the parentheses, so I prefer GitHub's style in particular this case.  However, the Seattle group does have a significant following.&lt;/p&gt;

&lt;p&gt;To me, this implies that it isn't likely worth a rule.  The reason doesn't seem to hold in enough cases to be considered good.  Both of these groups seem to be able to coexist in our community without creating two versions of every gem, just to rewrite the method definitions.  I say let it go.&lt;/p&gt;

&lt;p&gt;Now, both sides do agree to leave parentheses off of argument-less definitions and calls.  Perhaps that means that one is rule worthy.  It's definitely gray area though.&lt;/p&gt;

&lt;p&gt;Finally, with style guides, I think it's critical that we remember the most important thing I learned from &lt;em&gt;Smalltalk Best Practice Patterns&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code's primary purpose is to communicate with the reader.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you make a rule that violates that, it's instantly very suspect.  For example, GitHub's guide has this rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; keywords are banned. It's just not worth it. Always use &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; instead.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Of course, &lt;a href="http://mislav.uniqpath.com/2012/03/unless-else/"&gt;not everyone agrees&lt;/a&gt;.  The problem with this rule is that is that it makes Ruby less expressive.  Those operators actually &lt;a href="http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/"&gt;have a purpose&lt;/a&gt;.  Though it's not popular, I even use them in conditionals, because I think they read better.  &lt;em&gt;[&lt;strong&gt;Update&lt;/strong&gt;:  I was eventually swayed by Avdi's argument so I no longer do this.]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;GitHub is likely worried about how these operators interact with assignment statements.  It can trip up new Rubyists.  If you read Avdi's post though, you'll see that the precedence of these operators is actually why they exist.  Even my usage of them in conditionals is rarely a problem, since assignments are not common in conditionals.  Plus, if you do assign in a conditional, GitHub has a rule that fixes the precedence even with an &lt;code&gt;and&lt;/code&gt;/&lt;code&gt;or&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using the return value of &lt;code&gt;=&lt;/code&gt; (an assignment) is ok, but surround the assignment with parenthesis.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;That's a neat rule, if you ask me, because it allows for what I consider to be a natural programming construct in Ruby:  run this code if I can assign this variable.  For example:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unread?&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="c1"&gt;# ... work with message here...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But, the parentheses flag the reader that you haven't made the common bug of confusing &lt;code&gt;=&lt;/code&gt; with &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Getting back to the &lt;code&gt;and&lt;/code&gt;/&lt;code&gt;or&lt;/code&gt; case, I think education about these operators beats forbidding them.&lt;/p&gt;

&lt;h4&gt;What's Our Style?&lt;/h4&gt;

&lt;p&gt;Enough blathering on about how to make rules and why they exist.  Let's take a look at what's actually out there for us Rubyists.&lt;/p&gt;

&lt;h5&gt;We Agree About&lt;/h5&gt;

&lt;p&gt;I think it's safe to say that the majority of Ruby style guides agree about at least these points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An indent should be two spaces.&lt;/li&gt;
&lt;li&gt;Use spaces, not tabs.&lt;/li&gt;
&lt;li&gt;Keep lines at 80 characters in length or less.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;A lot of communities fight pretty heavily over these points, but the Ruby community seems to pretty universally agree.  The reasoning is also sound:  two spaces is enough to notice, but it leaves you plenty of room on the line.  Spaces make sure I see what you see, even if our editors have different settings.&lt;/p&gt;

&lt;p&gt;The last one surprises me a little.  I strongly believe in it, but I see a lot of Ruby code that doesn't.  Still almost all of the guides I checked had it in there.&lt;/p&gt;

&lt;p&gt;There were many more rules about spacing in the guides.  While I agreed with a lot of them, I'm not sure everyone would.  Are we certain sure it's &lt;code&gt;select { |arg| arg }&lt;/code&gt; instead of &lt;code&gt;select {|arg| arg }&lt;/code&gt;?  I use the former, but I see some of the latter and it doesn't throw me into a rage.  I'm just not sure I'm willing to burn a rule on stuff like that.&lt;/p&gt;

&lt;p&gt;We also seem to agree on how to name things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;snake_case&lt;/code&gt; for variable and method names, &lt;code&gt;CamelCase&lt;/code&gt; for classes and modules, and &lt;code&gt;ALL_CAPS_SNAKE_CASE&lt;/code&gt; for other constants.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The reasoning is pretty obvious.  This helps you tell what you are looking at right away.  It makes it much easier to read code.&lt;/p&gt;

&lt;p&gt;What else do most of the style guides care about?  How you indent your &lt;code&gt;case&lt;/code&gt; statements.  They definitely prefer this style:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Too young."&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Access granted."&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Too old."&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Malformed age."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or even:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
           &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;   &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Too young."&lt;/span&gt;
           &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;  &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Access granted."&lt;/span&gt;
           &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Too old."&lt;/span&gt;
           &lt;span class="k"&gt;else&lt;/span&gt;              &lt;span class="s2"&gt;"Malformed age."&lt;/span&gt;
           &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But never:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
  &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Too young."&lt;/span&gt;
  &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Access granted."&lt;/span&gt;
  &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Too old."&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Malformed age."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
             &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;   &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Too young."&lt;/span&gt;
             &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;  &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Access granted."&lt;/span&gt;
             &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s2"&gt;"Too old."&lt;/span&gt;
             &lt;span class="k"&gt;else&lt;/span&gt;              &lt;span class="s2"&gt;"Malformed age."&lt;/span&gt;
           &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;when&lt;/code&gt; statements need to line up with the &lt;code&gt;case&lt;/code&gt;.  The reasoning?  I have no idea, but it looks right to me!&lt;/p&gt;

&lt;h5&gt;The Parts I Found Interesting&lt;/h5&gt;

&lt;p&gt;The Google style guide mentioned using Ruby's block comments to hide headers and footers in your code.  For example:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="cm"&gt;=begin&lt;/span&gt;
&lt;span class="cm"&gt;  * Name:&lt;/span&gt;
&lt;span class="cm"&gt;  * Description:&lt;/span&gt;
&lt;span class="cm"&gt;  * Author:&lt;/span&gt;
&lt;span class="cm"&gt;  * Date:&lt;/span&gt;
&lt;span class="cm"&gt;  * License:&lt;/span&gt;
&lt;span class="cm"&gt;=end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I'm not sure how I feel about that.  I worry about programming constructs that can go off-screen.  To give an example, I don't like this method of declaring class methods:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Whatever&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;class_method&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The reason is that you could end up facing something line this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Whatever&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
    &lt;span class="c1"&gt;# a page of definitions here...&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;what_am_i&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="c1"&gt;# another page of definitions here...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you are centered on the definition of &lt;code&gt;what_am_i&lt;/code&gt;, the only way you can know it's defined as a class method is if you pick up on the extra indentation.  I don't think that communicates well, so I prefer the following technique, even if it does introduce some mild duplication:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Whatever&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_method_1&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_method_2&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Block comments are subject to the same off-screen issue, but a smart editor is going to save you with them.  It should syntax highlight the entire chunk as a comment, so you would still know what you are in the middle of.&lt;/p&gt;

&lt;p&gt;I doubt this is rule worthy in either case, but it's interesting to think about.&lt;/p&gt;

&lt;p&gt;I found the following rule from GitHub's guide interesting mainly because it nails my bad habit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefer single-quoted &lt;code&gt;String&lt;/code&gt;s when you don't need string interpolation or special symbols such as &lt;code&gt;\t&lt;/code&gt;, &lt;code&gt;\n&lt;/code&gt;, &lt;code&gt;'&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I don't do this.  Instead I use double-quoted &lt;code&gt;String&lt;/code&gt;s most of the time.  I switch to single-quoting only in rare cases where it reduces some escaping.  Even then, I'm just as likely to switch to a special &lt;code&gt;String&lt;/code&gt; form, like &lt;code&gt;%Q{...}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My reasons are pretty weak.  It's mainly that I too often found myself having to switch a single-quoted &lt;code&gt;String&lt;/code&gt; to double-quotes so I could introduce some interpolation.  It also helps in my editor, since I can have it always pair double-quotes while never pairing singles.  I'll be typing the former to make &lt;code&gt;String&lt;/code&gt;s but the latter for things like apostrophes, so this is generally perfect.&lt;/p&gt;

&lt;p&gt;The guide's rule is probably superior though, because it communicates with the reader that they are looking at a simple &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Dan Kubb's guide led me to read about a couple of interesting concepts that I hadn't thought about enough:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Try to have methods either return the state of the object and have no side effects, or return &lt;code&gt;self&lt;/code&gt; and have side effects. This is otherwise known as Command-query separation (CQS):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Command-query_separation"&gt;http://en.wikipedia.org/wiki/Command-query_separation&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do not program defensively.  (See &lt;a href="http://www.erlang.se/doc/programming_rules.shtml#HDR11"&gt;http://www.erlang.se/doc/programming_rules.shtml#HDR11&lt;/a&gt;.)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;GitHub has a rule I doubt is common:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Indent the &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;protected&lt;/code&gt;, and &lt;code&gt;private&lt;/code&gt; methods as much the method definitions they apply to. Leave one blank line above them.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here's the sample code they give for that:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;public_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;private_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I think it's more common to see a blank line below it as well:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;public_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;private_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I have also seen it "outdented" like this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;public_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;private_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That doesn't feel right to me though, since we indent other inline class method calls.  I do see how it kind of delimits a section though, much like &lt;code&gt;rescue&lt;/code&gt; would inside a method definition.  I think you can make the argument either way.&lt;/p&gt;

&lt;p&gt;There was another variant that you use to see a lot, but &lt;a href="https://github.com/rails/rails/commit/b88a181b7f296d89237bdb727ecc15cbfdf13b65"&gt;even Rails has decided it's wrong now&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;public_method&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;private_method&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Aaron Patterson says this is wrong because it's not how Emacs indents it.  I think he was joking at the time, but it turns out that &lt;a href="http://www.slideshare.net/yukihiro_matz/how-emacs-changed-my-life"&gt;he may be more right&lt;/a&gt; about that than we could have guessed.&lt;/p&gt;

&lt;p&gt;The first rule in Bozhidar Batsov's "Comments" section was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write self-documenting code and ignore the rest of this section. Seriously!&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I really like that.  The more programming experience I get the less respect I have for comments.  I do use annotations, like &lt;code&gt;TODO&lt;/code&gt; and &lt;code&gt;FIXME&lt;/code&gt; (and Bozhidar has rules for those too), but most other comments are suspect, in my opinion.&lt;/p&gt;

&lt;p&gt;The guides definitely disagreed heavily about which documentation scheme we should be using or if we should even be using one at all.&lt;/p&gt;

&lt;p&gt;I also learned a neat trick from Bozhidar's guide that I hadn't encountered before:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;When using &lt;code&gt;class_eval&lt;/code&gt; (or other &lt;code&gt;eval&lt;/code&gt;) with &lt;code&gt;String&lt;/code&gt; interpolation, add a comment block showing its appearance if interpolated (a practice I learned from the Rails code):&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="c1"&gt;# from activesupport/lib/active_support/core_ext/string/output_safety.rb&lt;/span&gt;
&lt;span class="no"&gt;UNSAFE_STRING_METHODS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;unsafe_method&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s1"&gt;'String'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;respond_to?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsafe_method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;class_eval&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class="no"&gt;EOT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;__LINE__&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="sh"&gt;      # ...&lt;/span&gt;

&lt;span class="sh"&gt;      def #{unsafe_method}!(*args)  # def capitalize!(*args)&lt;/span&gt;
&lt;span class="sh"&gt;        @dirty = true               #   @dirty = true&lt;/span&gt;
&lt;span class="sh"&gt;        super                       #   super&lt;/span&gt;
&lt;span class="sh"&gt;      end                           # end&lt;/span&gt;
&lt;span class="no"&gt;    EOT&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;But the most entertaining rule award definitely goes to Ryan Davis for this gem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don’t &lt;code&gt;rescue Exception&lt;/code&gt;.  EVER.  Or I will stab you.&lt;/li&gt;
&lt;/ul&gt;&lt;h5&gt;What Are They Just Wrong About?&lt;/h5&gt;

&lt;p&gt;I hate to say it, but some rules were just bad.  Let's look at a handful that bugged me.  First, I submit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not use single letter variable names.  Avoid uncommunicative names.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I don't even think the first half of that rule agrees with the second half of that rule.  Consider this code:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;ary&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each_index&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There's nothing wrong with that code.  &lt;code&gt;i&lt;/code&gt; is kind of the universal variable name for an index and as such it's even more communicative than a longer name, like &lt;code&gt;index&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid multi-line &lt;code&gt;?:&lt;/code&gt; (the ternary operator), use &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;unless&lt;/code&gt; instead.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;While I understand where this rule is coming from, it's just a bad idea to draw hard lines in the sand like this.  You're sure this is always wrong?  I'm not.&lt;/p&gt;

&lt;p&gt;Ruby 1.9 even supports a nice new syntax for this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;test?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;calc_true_result&lt;/span&gt;
                 &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;calc_false_result&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that Ruby allows those operators to line up, this can be a very readable construct, in my opinion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid using &lt;code&gt;$1-9&lt;/code&gt; as it can be hard to track what they contain.  Named groups can be used instead.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Almost all of the guides did contain a rule like this, but I see a lot of Ruby code and variables like &lt;code&gt;$1&lt;/code&gt; are extremely common.  The issue is that the &lt;code&gt;MatchData&lt;/code&gt; methods aren't all that more expressive and it can be awkward to get a hold of the object to use them on.&lt;/p&gt;

&lt;p&gt;I do like the named variables that this rule recommends (added in Ruby 1.9), but they can make a simple regex quite a bit wordier and introduce local variables in the recommended usage.  I don't think I'm comfortable saying that's always OK.&lt;/p&gt;

&lt;p&gt;I'm not really sure what the perceived problem is with &lt;code&gt;$1&lt;/code&gt; and friends either.  Programmers seem to understand them just fine.  Some may worry that they are evil globals, but the fact is that they are magic &lt;code&gt;Thread&lt;/code&gt; local variables that just do the right thing.&lt;/p&gt;

&lt;p&gt;Until I see a good reason for this rule, I'm not buying it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not use &lt;code&gt;inject&lt;/code&gt; in library code.  In app code it is ok.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I saw this rule on Dan Kubb's guide earlier this week, but it was already removed before I published this article.  I'm glad to see he's over this one.&lt;/p&gt;

&lt;p&gt;I think this stems from a fear of &lt;code&gt;inject()&lt;/code&gt; being a little slower.  It's true that it is.  Here's a benchmark that shows the problem:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"benchmark"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;each_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="n"&gt;sum&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inject_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;TESTS&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10_000_000&lt;/span&gt;
&lt;span class="no"&gt;NUMBERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="no"&gt;Benchmark&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bmbm&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;bench&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;bench&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"each_sum"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;TESTS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;each_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NUMBERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;bench&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"inject_sum"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;TESTS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;inject_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NUMBERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That shows the following on Ruby 1.9.3:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Rehearsal ----------------------------------------------
each_sum     7.440000   0.000000   7.440000 (  7.442971)
inject_sum  10.170000   0.010000  10.180000 ( 10.177593)
------------------------------------ total: 17.620000sec

                 user     system      total        real
each_sum     7.540000   0.000000   7.540000 (  7.546324)
inject_sum  10.220000   0.000000  10.220000 ( 10.217871)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, it's not that slow.  Many iterators have a similar penalty.  I had to use ten million iterations to get it to show up well.&lt;/p&gt;

&lt;p&gt;The situation use to be worse though.  It's more than twice as slow as &lt;code&gt;each&lt;/code&gt; on Ruby 1.8.7:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Rehearsal ----------------------------------------------
each_sum    23.340000   0.010000  23.350000 ( 23.354953)
inject_sum  51.260000   0.020000  51.280000 ( 51.279145)
------------------------------------ total: 74.630000sec

                 user     system      total        real
each_sum    23.730000   0.010000  23.740000 ( 23.734525)
inject_sum  52.320000   0.010000  52.330000 ( 52.323466)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So perhaps that's where this fear came from.&lt;/p&gt;

&lt;p&gt;In any event, don't avoid the iterators.  Use them for what they are for.  Remember, expressive code is always the first goal.&lt;/p&gt;

&lt;p&gt;If you find something to be too slow, you can always refactor for performance.  That should only be needed in extremely rare edge cases though.  Go for readability first.&lt;/p&gt;

&lt;p&gt;Hopefully this section reminds us all that we always need to treat these style guides as suggestions, not laws.  You're the programmer, so you're in charge.  No exceptions.&lt;/p&gt;

&lt;h4&gt;Worth a Read&lt;/h4&gt;

&lt;p&gt;I've dropped a ton of links in this article, but it really is worth taking a little time to read through at least some of these style guides.  They cover a ton of ideas, good and bad, that I didn't choose to rehash here.  If nothing else, I promise that they will get you thinking.&lt;/p&gt;

&lt;p&gt;If you are pressed for time, start with the GitHub guide.  It seems to reflect a lot of current thinking.  Some of it is controversial though, as I've noted.  If you have time for a second source, check out Dan Kubb's guide.  It is a good representation of the various forks and it contains some neat ideas, even if it does probably go a little too far in some areas.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
</feed>
