2011-11-29 142 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