Ruby中方法和public_methods之间的区别

时间:2022-04-15 22:28:05

I am just curious to know that what is exactly difference between methods and public_methods in Ruby? From RubyMonk Analysis section

我很想知道Ruby中方法和public_methods之间究竟有什么区别?来自RubyMonk Analysis部分

The methods method on Object allows us to tap into to the list of public methods available on an Object and its ancestors. This is equivalent to using public_methods. They return all the instance methods and the class methods belonging to that object and the ones accessible on that object's ancestors. If you want to ignore the ancestors and restrict the listing to just the receiver, you can pass in false to public_methods(false).

Object上的方法方法允许我们使用Object及其祖先上可用的公共方法列表。这相当于使用public_methods。它们返回属于该对象的所有实例方法和类方法以及该对象的祖先可访问的方法。如果要忽略祖先并将列表限制为仅接收者,则可以将false传递给public_methods(false)。

For curiosity, i also call methods(false) that return different output from public_methods(false)

为了好奇,我还调用从public_methods返回不同输出的方法(false)(false)

My sample code and output:

我的示例代码和输出:

p String.methods.size
p String.public_methods.size
p String.methods(false).size
p String.public_methods(false).size
p String.public_methods(false) - String.methods(false)

STDOUT:

235
235
3
19
[:json_create, :yaml_tag, :const_missing, :allocate, :new, :superclass, :cattr_reader, :cattr_writer, :cattr_accessor, :class_attribute, :superclass_delegating_accessor, :descendants, :subclasses, :duplicable?, :json_creatable?, :to_yaml] 

From above output i just see that methods and public_methods are not same but can't find out what is exactly difference between them.

从上面的输出我只看到方法和public_methods不相同,但无法找出它们之间的确切区别。

1 个解决方案

#1


In the Object class documentation for Object#public_methods:

在Object#public_methods的Object类文档中:

Returns the list of public methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.

返回obj可访问的公共方法列表。如果all参数设置为false,则仅列出接收器中的那些方法。

In contrast, the documentation for Object#methods state:

相比之下,Object#方法的文档说明:

Returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj’s ancestors. If the optional parameter is false, it returns an array of obj‘s public and protected singleton methods, the array will not include methods in modules included in obj.

返回obj的public方法和受保护方法的名称列表。这将包括obj的祖先可访问的所有方法。如果可选参数为false,则返回obj的public和protected singleton方法的数组,该数组将不包含obj中包含的模块中的方法。

So:

  1. #public_methods returns only the methods which are public, whereas #methods returns also (names of) protected methods.

    #public_methods仅返回公共方法,而#methods也返回受保护方法的名称。

  2. The false parameter has different effects. I'm not too sure about the scope of the difference, but it seems to me that the main difference would be singleton methods vs. instance methods*.

    false参数具有不同的效果。我不太确定差异的范围,但在我看来,主要区别在于单例方法与实例方法*。


*'instance methods' can also refer to class methods if class is the receiver - as everything is an object and, as stated in the Class documentation:

*'instance methods'也可以引用类方法,如果class是接收者 - 因为一切都是对象,并且如类文档中所述:

Classes in Ruby are first-class objects---each is an instance of class Class.

Ruby中的类是第一类对象---每个类都是类Class的实例。

#1


In the Object class documentation for Object#public_methods:

在Object#public_methods的Object类文档中:

Returns the list of public methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.

返回obj可访问的公共方法列表。如果all参数设置为false,则仅列出接收器中的那些方法。

In contrast, the documentation for Object#methods state:

相比之下,Object#方法的文档说明:

Returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj’s ancestors. If the optional parameter is false, it returns an array of obj‘s public and protected singleton methods, the array will not include methods in modules included in obj.

返回obj的public方法和受保护方法的名称列表。这将包括obj的祖先可访问的所有方法。如果可选参数为false,则返回obj的public和protected singleton方法的数组,该数组将不包含obj中包含的模块中的方法。

So:

  1. #public_methods returns only the methods which are public, whereas #methods returns also (names of) protected methods.

    #public_methods仅返回公共方法,而#methods也返回受保护方法的名称。

  2. The false parameter has different effects. I'm not too sure about the scope of the difference, but it seems to me that the main difference would be singleton methods vs. instance methods*.

    false参数具有不同的效果。我不太确定差异的范围,但在我看来,主要区别在于单例方法与实例方法*。


*'instance methods' can also refer to class methods if class is the receiver - as everything is an object and, as stated in the Class documentation:

*'instance methods'也可以引用类方法,如果class是接收者 - 因为一切都是对象,并且如类文档中所述:

Classes in Ruby are first-class objects---each is an instance of class Class.

Ruby中的类是第一类对象---每个类都是类Class的实例。