3

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

wattostudios
  • 8,666
  • 13
  • 43
  • 57
RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193

4 Answers4

10

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.

Benoit Garret
  • 14,027
  • 4
  • 59
  • 64
  • 2
    Perhaps you should change your example to something like `''.public_methods` or `[].public_methods` instead. It's obvious to anyone who knows Ruby that your example lists the methods that the `Array` class object *itself* responds to and *not* the instance methods of the `Array` class, but it might be misleading to novices. – Jörg W Mittag Nov 15 '11 at 16:31
  • @JörgWMittag thanks for the input. I went with a custom class because the Array or String instance have too many methods and the SO code blocks don't wrap lines. It wasn't immediately clear the results weren't the same. – Benoit Garret Nov 15 '11 at 16:56
1

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]
Sean Vikoren
  • 1,084
  • 1
  • 13
  • 24
0

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

Community
  • 1
  • 1
BF4
  • 1,076
  • 7
  • 23
-1

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.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33