TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Ruby Style Guide

77 pointsby garrettdimonalmost 6 years ago

11 comments

pjungwiralmost 6 years ago
Oh fun another style discussion! :-)<p>My own taste is for a &quot;book style&quot;: write the code like you see in good books. I agree with most of this guide, although I&#x27;m pretty loose and case-by-case in practice. I&#x27;ve gradually added rubocop to lots of my projects, but then I wind up disabling tons of the rules.<p>I&#x27;m sad all the Ruby guides I&#x27;ve seen ban &quot;and&quot; and &quot;or&quot;. I don&#x27;t find them to be such an issue, and they are nicer to type &amp; read. For one thing you can say `foo or raise &quot;msg&quot;` but need parens to say `foo || raise(&quot;msg&quot;)`. On team projects I sigh and go along with it, but it is a shame.<p>Speaking of &quot;book style&quot;: I have a bunch of Python books with atrocious style, e.g. no spaces between operators or even between function arguments. How can anyone read that? I even see this from high-quality publishers like O&#x27;Reilly. What is going on with the Python world? Are their thumbs tired from all that indentation, so they leave out spaces everywhere else? ;-)<p>I really want to write a SQL style guide. There seems to be almost no consensus. My own formatting is idiosyncratic and not super principled, but I feel it is the most readable and expresses structure the best. It looks like this:<p><pre><code> SELECT a, b FROM t1 LEFT OUTER JOIN t2 ON t2.t1_id = t1.id WHERE t2.c = &#x27;foo&#x27;; </code></pre> (EDIT: Sorry, t2.c = &#x27;foo&#x27; is a pretty dumb thing to do in an outer join. :-)<p>Other people like to do one or more of these things:<p><pre><code> SELECT a , b FROM t1 LEFT OUTER JOIN t2 ON t2.t1_id = t1.id WHERE t2.c = &#x27;foo&#x27;; </code></pre> In other words:<p>- lead with the comma in SELECT.<p>- ragged left edge but line up all the single spaces in a column.<p>- indent the joins.<p>Anyway sorry for the rambling. :-) There is some observation that committees spend 90% of their time on trivial details and hardly discuss any of the important stuff, and style is no exception. But who doesn&#x27;t like giving their opinion? :-)
评论 #20185128 未加载
评论 #20184387 未加载
评论 #20184661 未加载
评论 #20187915 未加载
评论 #20185430 未加载
danmaz74almost 6 years ago
The only thing I really don&#x27;t like here is non indenting when inside case. I know that technically it&#x27;s not a block, but in practice I find it much less readable
评论 #20185476 未加载
steveklabnikalmost 6 years ago
I made a comment on this thread[1] in 2014, and it was closed two days ago. I wondered why... guess this is why!<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;rubocop-hq&#x2F;ruby-style-guide&#x2F;issues&#x2F;273" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rubocop-hq&#x2F;ruby-style-guide&#x2F;issues&#x2F;273</a>
评论 #20184822 未加载
评论 #20184629 未加载
评论 #20209823 未加载
intsunnyalmost 6 years ago
I&#x27;ll never understand Ruby&#x27;s obsession with implicit return.<p>When reading code, my eye will immediately notice the `return` keyword and will have less to think about in terms of code execution.<p>The `return` keyword makes for great readability.
评论 #20186761 未加载
onnnonalmost 6 years ago
There&#x27;s also StandardRB, it&#x27;s like StandardJS but for ruby:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;testdouble&#x2F;standard" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;testdouble&#x2F;standard</a><p>Lightning talk about it here:<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=uLyV5hOqGQ8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=uLyV5hOqGQ8</a><p>Edit: I posted it on its own if you want to discuss:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20186231" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20186231</a>
h1dalmost 6 years ago
Hard to swallow without any reasons written behind each decisions.
评论 #20184278 未加载
评论 #20184741 未加载
hdermsalmost 6 years ago
All languages should have gofmt at this point. Debating code style is a waste of everyone&#x27;s time.
weeksiealmost 6 years ago
Parens for method calls should only be used when things get ambiguous. Nothing bugs me more than seeing ruby code cluttered up with that garbage. I know, I know, but that&#x27;s my personal bikeshed and I&#x27;ll happily die on the roof of it.
pswensonalmost 6 years ago
80 char line length in 2019?
评论 #20185641 未加载
评论 #20187595 未加载
评论 #20184727 未加载
评论 #20186874 未加载
abakusalmost 6 years ago
How about 1 space indentation? Just to be able to squeeze even more stuff into the 80 char limit? 0 space is a bit too extreme :)
tigreznoalmost 6 years ago
Spaces for identation? seriously?
评论 #20185216 未加载