行,所以我想聲明,將接受PARAMS這樣我如何接受紅寶石上的這些參數?
比方說,我的方法是custom_method名爲
的方法@variable.custom_method(Profile, :as => @user)
我的方法應該抓住變量@user並調用@user.profile
但我怎麼捕獲第二個參數,這是一個散列
行,所以我想聲明,將接受PARAMS這樣我如何接受紅寶石上的這些參數?
比方說,我的方法是custom_method名爲
的方法@variable.custom_method(Profile, :as => @user)
我的方法應該抓住變量@user並調用@user.profile
但我怎麼捕獲第二個參數,這是一個散列
更簡單的解決方案可能是:
def custom_method(association, args)
obj = args[:as] or fail("Missing argument :as => obj")
obj.send(association.name.underscore)
...
end
更豐富的函數調用,但缺點很明顯:方法簽名會丟失信息。
在方法簽名中聲明一個參數? – 2012-08-09 23:19:27