Is there a way in Ruby to print out the public met

2019-07-20 12:34发布

问题:

... without including all of the public methods of the generic Object? I mean, other than doing array subtraction. I just want to quickly review what's available to me from an object sometimes without going to the docs.

回答1:

methods, instance_methods, public_methods, private_methods and protected_methods all accept a boolean parameter to determine if the methods of your object's parents are included.

For example:

ruby-1.9.2-p0 > class MyClass < Object; def my_method; return true; end; end;
ruby-1.9.2-p0 > MyClass.new.public_methods
 => [:my_method, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] 
ruby-1.9.2-p0 > MyClass.new.public_methods(false)
 => [:my_method]

As noted by @Marnen, the methods defined dynamically (eg. with method_missing) won't appear here. Your only bet for these ones is hoping that the libraries you're using are well documented.



回答2:

Is this the result you were looking for?

class Foo
  def bar
    p "bar"
  end
end

p Foo.public_instance_methods(false) # => [:bar]


ps I expect this was not the result you were after:

p Foo.public_methods(false)          # => [:allocate, :new, :superclass]


回答3:

If there were, it wouldn't be terribly useful: often, the public methods are not your only options, due to Ruby's ability to fake methods by dynamic metaprogramming. So you can't really rely on instance_methods to tell you much that's useful.



回答4:

I started to try to document all these inspection methods at one point in https://github.com/bf4/Notes/blob/master/code/ruby_inspection.rb

As noted in other answers:

class Foo; def bar; end; def self.baz; end; end

Firstly, I like to sort the methods

Foo.public_methods.sort # all public instance methods
Foo.public_methods(false).sort # public class methods defined in the class
Foo.new.public_methods.sort # all public instance methods
Foo.new.public_methods(false).sort # public instance methods defined in the class

Useful tip Grep to find out what your options are

Foo.public_methods.sort.grep /methods/ # all public class methods matching /method/
# ["instance_methods", "methods", "private_instance_methods", "private_methods", "protected_instance_methods", "protected_methods", "public_instance_methods", "public_methods", "singleton_methods"]
Foo.new.public_methods.sort.grep /methods/
#  ["methods", "private_methods", "protected_methods", "public_methods", "singleton_methods"]

Also see https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick