【发布时间】:2019-07-02 05:39:35
【问题描述】:
我在 erb 库中遇到过以下代码。注意双(( )):
class MyTest
def location=((filename, lineno))
@filename = filename
@lineno = lineno if lineno
end
end
下面的locatia=方法是另一个版本,没有(( ))进行测试:
class MyTest
def locatia=(filename, lineno)
@filename = filename
@lineno = lineno if lineno
end
end
我得到了这个结果:
a = MyTest.new
a.location = "foo", 34
a # => #<MyTest:0x2a2e428 @filename="foo", @lineno=34>
b = MyTest.new
b.location = "foo"
b # => #<MyTest:0x2a2e338 @filename="foo">
c = MyTest.new
c.locatia = "foo", 34
c # >> `locatia=': wrong number of arguments (given 1, expected 2) (ArgumentError)
带双括号的版本可以正常工作。单人失败。它必须在源代码的某个级别上指定。有什么线索吗?
【问题讨论】:
标签: ruby methods parameters