{ } V.S. DO / END
最後由 eddie 於2011-12-19 13:27:13 +0800回應
, 795次閱讀
http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
在 Ruby 中使用迴圈的方式有很多種。
你可以使用 {} ( braces) 或者是 do / end。
Jim Weirich 關於這個主題他是建議這樣使用的:
* Use { }
for blocks that return values
* Use do / end
for blocks that are executed for side effects
This has the advantage of using the choice of block delimiter to convey a little extra information. Here’s some examples.
# block used only for side effect
list.each do |item| puts item end
# Block used to return test value
list.find { |item| item > 10 }
# Block value used to build new value
list.collect { |item| "-r" + item }
我個人是覺得蠻不錯的 guideline
截至 2011-12-19 13:27:13 +0800,共收到 1 條回應
eddie
1樓, 於2011-12-19 13:27:13 +0800回應
http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/
半年前有一篇,供大家參考 :)