3
A
回答
1
<=> the "spaceship" or comparison operator
=== the "trequals" or case matching operator
5
方法簽名末尾的&符將抓住並期待一個塊。
def foo(bar, &block)
block.call (bar += 1)
end
的符號也可以以這種形式用於調用to_proc
,讓調用:address
方法用一個符號(例如是從其他地方借來)
@webs ||= Web.find(:all).index_by &:address
像+=
和-=
快捷操作便利。
不是一個運算符,而是Rails使得另一個捷徑成爲可能。這將讓你欄時,foo是要麼nil?
或false
a = foo || bar
在「經營者」的方面,我發現了一個非官方的東西在這裏以供參考:Ruby operators
+0
謝謝。 foo ||酒吧對我來說很方便 – AntonAL 2011-03-03 07:39:21
5
我發現splat operator是的一個最棘手的Ruby操作符:
它拆分數組:
a,b,c = *[1,2,3]
或者建立陣列:
*a = 1,2,3
它還可在case
語句中使用:
first = ["one", "two"]
second = ["three", "four"]
case number
when *first
"first"
when *second
"second"
end
它可以被用作用於可變參數函數的參數:
def stuff *args
args.join('|')
end
由於它被用於兩者(分裂和創建數組),我總是必須在使用它之前檢查語法。它可以用於很多目的(如將哈希轉換爲數組),我真的覺得很難掌握。
相關問題
- 1. Ruby的&&運營商
- 2. 整數運營商
- 3. Ruby:<< - 運營商
- 4. Ruby on Rails:運營商
- 5. 整蠱SQL運動
- 6. 空 - 結合運營商和運營商&&
- 7. 運營商的情況下運營商
- 8. 或運營商與三元運營商
- 9. 的JavaScript +運營商VS - 運營商
- 10. 比較運營商(飛船運營商)如何在三元運營商處理
- 11. Ruby的等價於C#的??運營商
- 12. Ruby的&&運營商關於陣列
- 13. 運營商OCaml中
- 14. ++運營商在R
- 15. ??運營商在SQL
- 16. 運營商在python
- 17. = - 運營商在java中
- 18. 運營商=在C++中
- 19. Javascript:?運營商
- 20. R%*%運營商
- 21. instanceof運營商
- 22. Bigint +運營商
- 23. Groovy *。運營商
- 24. '運營商='
- 25. jQuery @運營商?
- 26. ??運營商system.DBNull
- 27. 運營商
- 28. 令運營商
- 29. &&運營商JSONpath()
- 30. Javascript +運營商
http://stackoverflow.com/questions/63998/hidden-features-of-ruby。至少請檢查標籤Ruby的FAQ部分:http://stackoverflow.com/questions/tagged/ruby?sort=faq&pagesize=50 – sorens 2011-02-27 17:50:46