ruby 访问修饰符,不同版本的不同输出 2.5 - 2.7

问题描述

我正在尝试运行此代码,它使用不同版本的 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​​ 的第一个输出是正确的.. 有任何反馈吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)