New to ruby, put on your newbie gloves.
Is there any difference (obscure or practical) between the following two snippets?
my_array = [:uno, :dos, :tres]
my_array.each { |item|
puts item
}
my_array = [:uno, :dos, :tres]
my_array.each do |item|
puts item
end
I realize the brace syntax would allow you to place the block on one line
my_array.each { |item| puts item }
but outside of that are there any compelling reasons to use one syntax over the other?
Ruby cookbook says bracket syntax has higher precedence order than
do..end
Second example only works when parentheses is used,
1.upto(3) { |x| puts x }
This is a bit old question but I would like to try explain a bit more about
{}
anddo .. end
like it is said before
but how this one makes difference:
in this case, method1 will be called with the block of
do..end
and method2 will be passed to method1 as an argument! which is equivalent tomethod1(method2){ puts "hi" }
but if you say
then method2 will be called with the block then the returned value will be passed to method1 as an argument. Which is equivalent to
method1(method2 do puts "hi" end)
#### output ####
There are two common styles for choosing
do end
vs.{ }
for blocks in Ruby:The first and very common style was popularized by Ruby on Rails, and is based on a simple rule of single vs. multi-line:
{ }
for single-line blocksdo end
for multi-line blocksThis makes sense because do/end reads badly in a one-liner, but for multi-line blocks, leaving a closing
}
hanging on its own line is inconsistent with everything else that usesend
in ruby, such as module, class & method definitions (def
etc.) and control structures (if
,while
,case
, etc.)The second, less-frequently seen style is known as semantic, or "Weirich Braces", proposed by the late, great rubyist Jim Weirich:
do end
for procedural blocks{ }
for functional blocksThis means that when the block is evaluated for its return value, it should be chainable, and the
{}
braces make more sense for method chaining.On the other hand, when the block is evaluated for its side-effects, then the return value is of no consequence, and the block is just "doing" something, so it does not make sense to be chained.
This distinction in the syntax conveys visual meaning about the evaluation of the block, and whether or not you should care about its return value.
For example, here the return value of the block is applied to every item:
However, here it's not using the block's return value. It's operating procedurally, and doing a side-effect with it:
Another benefit of the semantic style is that you don't need to change braces to do/end just because a line was added to the block.
As an observation, coincidentally functional blocks are frequently a one-liner, and procedural blocks (e.g. config) are multi-line. So, following the Weirich style ends up looking almost the same as the Rails style.
Generally, the convention is to use
{}
when you are doing a small operation, for example, a method call or a comparison, etc. so this makes perfect sense:But if you have slightly complex logic that goes to multiple lines then use
do .. end
like:Basically, it comes down to, if your block logic goes to multiple lines and cannot be fitted on the same line then use
do .. end
and if your block logic is simple and just a simple/single line of code then use{}
.