x無法存取,你可能沒有權限或未登入。
19e786a2a74377ff6e052d87fd8d1fa8

{ } V.S. DO / END

2011-12-19 11:21:09 +0800xditeRuby 節點 中發起
最後由 eddie2011-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 條回應
F2dcf6633971844e19ca96ea294ba976
eddie 1樓, 於2011-12-19 13:27:13 +0800回應

http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/
半年前有一篇,供大家參考 :)

需要 登入 後方可回應,如果你還沒有帳號按這裡 註冊