# Public_methods object not working properly

I am creating a new module:

module M
  def self.g
  end
end

I would like to list all the (class-) methods defined in M. Of course, M.public_methodsalso listed the methods from Object, so I tried:

M.public_methods(false)

but when I try this in irb, it still lists a number of additional methods:

M.public_methods(false)
# => [:g, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, …]

I can get the result I want to use

M.public_methods(false) - Object.public_methods

what surprises me. Should the parameter falsealready get rid of all the methods Object?

+4
source share
1 answer

Should the parameter falseget rid of all the methods Object?

Object#public_methods, , . , all false, , - . , - M singleton class (g) Module.

, M.public_methods(false) Module.public_instance_methods(false):

M.public_methods(false) - Module.public_instance_methods(false)
# => [:g]

M.public_methods(false) & Module.public_instance_methods(false) == 
  Module.public_instance_methods(false)
# => true

Object#singleton_methods , :

M.singleton_methods(false)
# => [:g]
+3

Source: https://habr.com/ru/post/1612532/


All Articles