6
可能重複:
Ruby block and unparenthesized arguments
What is the difference or value of these block coding styles in Ruby?爲什麼不做/ end和{}總是等價的?
我始終認爲,以下只是兩個說同樣的事情的方法:
[1,2,3].collect{|i| i * 2}
[1,2,3].collect do |i|
i * 2
end
但我在兩個語法中的一個ERB模板中出現一些奇怪的行爲ES似乎做了兩件不同的事情。此代碼的偉大工程:
<%=raw @menu.collect { |m|
content_tag("li") {
link_to(m.capitalize, url_for(:controller => m))
}
} %>
,但是當我重新寫爲:
<%=raw @menu.collect do |m|
content_tag("li") do
link_to(m.capitalize, url_for(:controller => m))
end
end %>
...我只是結束了我的@menu項目的連接字符串。我錯過了什麼嗎?這裏有一些小小的糖化語言嗎?
這是[紅寶石塊語法錯誤(http://StackOverflow.Com/q/6854283/),[代碼塊傳遞給'一個重複each'與括號工作,但不與'do'- 'end'(ruby)](http://StackOverflow.Com/q/6718340/),[塊定義 - 大括號和'''''''end'之間的區別?](http://StackOverflow.Com/q/ 6179442 /),[沒有'do''結束的Ruby多行塊]](http://StackOverflow.Com/q/3680097/),[使用'do'塊vs括號'{}'](http:// StackOverflow (http://stackoverflow.Com/q/533008/)... –
...和[Ruby block and unparenthesized arguments] (http://StackOverflow.Com/q/420147/)。 Ack! –
Ack!現在我被撕裂了。要刪除還是不刪除?有4個贊成,也許我的措辭對某人有用? –