difference between methods and public_methods in Ruby -
i curious know difference between methods
, public_methods
in ruby? rubymonk analysis section
the methods method on object allows tap list of public methods available on object , ancestors. equivalent using public_methods. return instance methods , class methods belonging object , ones accessible on object's ancestors. if want ignore ancestors , restrict listing receiver, can pass in false public_methods(false).
for curiosity, call methods(false)
return different output public_methods(false)
my sample code , 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 see methods
, public_methods
not same can't find out difference between them.
in object class documentation object#public_methods
:
returns list of public methods accessible obj. if parameter set false, methods in receiver listed.
in contrast, documentation object#methods
state:
returns list of names of public , protected methods of obj. include methods accessible in obj’s ancestors. if optional parameter false, returns array of obj‘s public , protected singleton methods, array not include methods in modules included in obj.
so:
#public_methods
returns methods public, whereas#methods
returns (names of) protected methods.the
false
parameter has different effects. i'm not sure scope of difference, seems me main difference singleton methods vs. instance methods*.
*'instance methods' can refer class methods if class receiver - object and, stated in class documentation:
classes in ruby first-class objects---each instance of class class.
Comments
Post a Comment