在Ruby中声明“私有”/“受保护”时实际发生了什么?

Ruby类定义中声明private / protected时实际发生了什么?它们不是 keywords,所以这意味着它们必须是方法调用,但我无法找到它们的定义位置.它们似乎没有记录.声明私有/受保护方法(如下所示)的两种不同方式是否以不同方式实现? (第二种方式显然是方法调用,但这在第一种方式中并不那么明显.)
class Foo
  private
  def i_am_private; end
  def so_am_i; end
end

class Foo
  def i_am_private; end
  def so_am_i; end
  private :i_am_private,:so_am_i
end

解决方法

两者都是方法调用.引自 docs

Each function can be used in two different ways.

  1. If used with no arguments,the three functions set the default access control of subsequently defined methods.
  2. With arguments,the functions set the access control of the named methods and constants.

请参阅此处的文档

> Module.private
> Access Control

您正在寻找Module.private方法如何存在.关于它,Here is where that happenshere is some more information.从class.c中定义的rb_define_private_method开始,您必须阅读更多内容.

希望有所帮助!

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...