【发布时间】:2021-06-18 20:25:09
【问题描述】:
我正在尝试运行此代码,它使用不同版本的 ruby 2.5 - 2.7 给出不同的输出
代码:
class ParentClass
def the_public_method
self.method1
end
private
def method1
puts "The private has been called"
end
end
class ChildClass < ParentClass
def test
self.method1
end
end
ParentClass.new.the_public_method
ChildClass.new.test
在 ruby 2.5 上提供:
Traceback (most recent call last):
1: from main.rb:19:in `<main>'
main.rb:3:in `the_public_method': private method `method1' called for
#<ParentClass:0x000056367ee0b388> (NoMethodError)
Did you mean? method
methods
exit status 1
在 ruby 2.7 上提供:
The private has been called
The private has been called
我认为旧版本的 ruby 的第一个输出是正确的。 有任何反馈吗?
【问题讨论】:
标签: ruby access-modifiers