Which style is preferred? Is there a good reason for one vs. the other?
Thanks in advance!
1) cmds.each do |cmd|
end
2) cmds.each { |cmd|
}
Example code:
cmds = [ "create", "update", "list", "help" ]
# Block style one
#
cmds.each do |cmd|
puts "loop1, cmd: #{cmd}"
end
# Block style two
#
cmds.each { |cmd|
puts "loop2, cmd: #{cmd}"
}
Conventionally, using
{}
is reserved for single-line blocks, whiledo...end
is for multi-line blocks.Because of how
{}
are not used in Ruby as often as in other languages like C++ or Javascript, usingdo...end
when given the chance instead of{}
can help make your code more readable for other Rubyists.Note that
{}
has precedence overdo...end
.