對不起,我甚至不知道如何提出這個問題..所以,如果你可以建議周圍,以及它,將不勝感激。如何使用新的ruby(1.9)哈希語法從關聯中創建符號(哈希鍵)?
# A. WORKS, but "stockroom" is hardcoded
render partial: association.to_s.singularize + '',
locals: {stockroom: new_object}
# B. WORKS, but uses old syntax
render partial: association.to_s.singularize + '',
locals: {association.to_s.singularize.to_sym => new_object}
# C. does NOT work
render partial: association.to_s.singularize + '',
locals: {association.to_s.singularize.to_sym: new_object}
# D. does NOT work
ass = association.to_s.singularize.to_sym
logger.debug "--- ass: #{ass.inspect} (#{ass.class})"
# => --- ass: :stockroom (Symbol)
render partial: association.to_s.singularize + '', locals: {ass: new_object}
這並不是說舊的語法是一件壞事,我只是想知道是否有辦法做到這一點使用新的語法(甚至通過中間(即屁股))。
我的猜測(只是一個猜測)是沒有;它實際上是一個句法竅門,即使你想對它進行評估,也可以將「:」的左邊的任何內容轉換爲符號。 – 2012-01-09 23:09:22