Ruby - 方法参数

时间:2011-11-29 04:51:44

标签: ruby

我的方法:

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)
                                      ^  

有什么问题?

2 个答案:

答案 0 :(得分:4)

带后缀标点符号=的方法只能有一个参数。

否则,您必须使用send来调用多个参数。

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

答案 1 :(得分:0)

使用=句法糖调用方法时不要使用括号。

像这样调用它:

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