2011-11-29 141 views
2

我的方法:紅寶石 - 方法參數

def my_method=(attributes, some_option = true, another_option = true) 
    puts hello 
end 

當我嘗試打電話,我得到這樣的錯誤:

my_method=({:one => 'one', :two => 'two'}, 1, 1) 
#you_code.rb:4: syntax error, unexpected ',', expecting ')' 
#my_method=({:one => 'one', :two => 'two'}, 1, 1) 
            ^ 

什麼問題?

回答

4

帶後綴標點符號=的方法只能有一個參數。

否則,您必須使用send來調用多個參數。

send :'my_method=', {:a => 1}, 1, 1 
+0

那你覺得這個怎麼樣[軌道屬性=方法(http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method -i-attributes-3D)? –

+0

@VladimirTsukanov已更新 – ShiningRay

+0

@VladimirTsukanov並且「guard_protected_attributes參數現在已被棄用」 – ShiningRay

0

不要在調用使用=語法糖的方法時使用括號。

調用它像這樣:

mymethod= {:one => 'one', :two => 'two'}, 1, 1

+0

這將返回一個數組。 – ShiningRay