Ruby 中这些块编码样式的区别或价值是什么? [英] What is the difference or value of these block coding styles in Ruby?
本文介绍了Ruby 中这些块编码样式的区别或价值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪种风格更受欢迎?一个与另一个有充分的理由吗?
Which style is preferred? Is there a good reason for one vs. the other?
提前致谢!
1) cmds.each do |cmd|
end
2) cmds.each { |cmd|
}
示例代码:
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}"
}
推荐答案
rails 团队和许多其他 rubyists 更喜欢对单行块使用花括号,对多行块使用do...end
.
The rails team and many other rubyists prefer to use curly braces for one line blocks and do...end
for multi-line ones.
两者之间唯一的功能区别是 do...end
块的优先级低于 {...}
块的优先级.
The only functional difference between the two is that the precedence of a do...end
block is lower than that of a {...}
block.
这篇关于Ruby 中这些块编码样式的区别或价值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文