difference between methods and public_methods in Ruby

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

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).

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

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)


[: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.

Source: New Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.