4 ms·
I might be missing some Ruby context here, but why does the tutorial show the {} syntax and later the do...end syntax like do...end is somehow better? (And then
by rhacker 6y ago
I might be missing some Ruby context here, but why does the tutorial show the {} syntax and later the do...end syntax like do...end is somehow better? (And then in the shoes4 repo it also shows do...end syntax as an example there).
Maybe I'm just used to {} but is do...end superior to most ruby devs?
- rahoulb 6y agoI've always worked with the convention that {} is for single line blocks and do ... end for multi-line
- Lio 6y agoI've also heard people discuss a convention where do end is used for side affecting code and {} is used for pure blocks.... but I've never seen anyone really stick to it so single-line vs multi-line is as good as anything.
- hashmal 6y agoThey don't have the same precedence. foo bar { ... } # bar receives the block foo bar do ... end # foo receives the block The later, paired with the non-mandatory parentheses for calls, allows to read and write some really neat stuff. The former is most often used for one-liners indeed.
- bdcravens 6y agoAs others have mentioned do/end if preferable for multi-line blocks. I assume that's due to Ruby's extensive use of hashes (which are almost always multi-line) and to keep a clear distinction between the two.