Ruby code blocks: do ... end vs. {...} (poll)

(1) Read the following …

(2) After reading the above explanation, please indicate your preference below:


Which of the two block styles do you prefer in code ?

  • Rails Style - single line {}, multiline do/end
  • Semantic / Weirich Style - functional {}, procedural do/end
0 voters

(3) Reply with any additional comments …

I have to admit that I wasn’t using any particular “system” in deciding
when to use doend and when to use {}.

I was generally favoring curly braces because of precedence, but not actually using any syntax that would cause precedence to be a factor.

Sometimes I’d just switch back and forth between the types when the code had multiple nested blocks, … thinking perhaps it helped to not cause hard to read code.

So after reading the explanation by Mr. Vit, the semantic style really makes the most sense to me. Now to only remember to employ it whilst coding …

I find the semantic style to be intriguing but seems like cognitive overhead when coming up with a solution to a problem when you’re not quite sure how the block will be used yet.

1 Like

My feeling is that you can practice these subtle things in your spare time until they become second nature, but you shouldn’t waste time fretting over the fine points while coding. Do what seems clearest at the time. It will probably still seem clearest later, and can easily be revised if you change your mind!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.