【发布时间】:2011-07-09 10:27:16
【问题描述】:
我知道 Ruby 中的 x == y 被解释为 a.==(y)。我尝试检查是否可以使用自定义方法 foo 实现相同的效果,如下所示:
class Object
def foo(n)
self == n
end
end
class A
attr_accessor :x
end
a = A.new
a.x = 4
puts a.x.==(4) # => true
puts a.x.foo(4) # => true
puts a.x == 4 # => true
puts a.x foo 4 # => in `x': wrong number of arguments (1 for 0) (ArgumentError)
很遗憾,这不起作用。我错过了什么? == 是 Ruby 中的特殊方法吗?
【问题讨论】: