我的工作是填写下面的__
(等于self.my_method...
)。
def my_method_in_the_same_class(a, b)
a * b
end
def test_calling_methods_in_same_class_with_explicit_receiver
assert_equal __, self.my_method_in_the_same_class(3,4)
end
我得到NoMethodError: private method 'my_method_in_the_same_class' called for main:Object
。正确的答案是12,我不知道为什么会出现这个错误。我可以输入12,但我想这不是学习的重点。
答案 0 :(得分:0)
您无法使用self
前缀调用私有方法,您应该将它们声明为protected
,或者使用#send
方法来调用它(正如Dave所说)。例如,在方法声明后添加:
protected :my_method_in_the_same_class
答案 1 :(得分:0)
无论你放入__
中的内容,错误都不会消失。错误不是来自那里。
您的方法在main
环境中定义,这使方法变得私有。不过,您使用self
来调用该方法。这是错误的原因。即使它不是私密的,也没有理由在这里使用self
。