Aman King

A Ruby object's...Aman King's BlikiControl PanelChange LogBrowse PagesSearch?

A Ruby object's own methods

Often times, when I'd like to check what all an object can do, I'd fire the following in irb:

obj = # or whatever
or better yet, if I have some idea about what I'm looking for, say a method with the word "sec", I'd type:
obj.methods.grep /sec/

The reason I have to do the above is that I want to avoid listing all the methods that the object can respond to, including ==, <==>, etc.

A quick way that I've come up with to list just the methods defined for that object, ignoring inherited ones ('noise'), is:

class Object
 def own_methods
   (methods - (self.class.ancestors - [self.class]).collect { |k| k.instance_methods }.flatten).sort

Now, to look up an object's own methods was as simple as:


Next I decided to make this a bit more flexible such that you can specify the number of levels of ancestors you want to include in the listing. Also, it should work for getting class methods for a class and not just instance methods. And so finally after some test-driving, this is what I came upon:

class Object
 def own_methods(levels_up = 0)
   tree, methods_to_exclude = self.kind_of?(Class) ? [self.ancestors, :methods] : [self.class.ancestors, :instance_methods]
   inherited_methods = tree[levels_up + 1, tree.size].collect { |k| k.send(methods_to_exclude) }.flatten
   methods - inherited_methods
if __FILE__ == $0
  require 'test/unit'
  class OwnMethodsTest < Test::Unit::TestCase
    def setup
      grandparent_class = do
        def instance_method_from_grandparent; end
        def self.class_method_from_grandparent; end
      parent_class = do
        def instance_method_from_parent; end
        def self.class_method_from_parent; end
      @klass = do
        def an_instance_method; end
        def self.a_class_method; end
    def test_own_instance_methods
      assert_equal ["an_instance_method"],
      assert_equal ["an_instance_method", "instance_method_from_parent"].sort,
      assert_equal ["an_instance_method", "instance_method_from_parent", "instance_method_from_grandparent"].sort,
    def test_own_class_methods
      assert_equal ["a_class_method"], @klass.own_methods
      assert_equal ["a_class_method", "class_method_from_parent"].sort, @klass.own_methods(1).sort
      assert_equal ["a_class_method", "class_method_from_parent", "class_method_from_grandparent"].sort, @klass.own_methods(2).sort

Here is how this is now helpful to me in irb:

require 'ownmethods'
require 'date'
DateTime.ancestors # => [DateTime, Date, Comparable, Object, Kernel]
DateTime.own_methods.sort # => ["now"] # => ["hour", "min", "new_offset", "newof", "of", "offset", "sec", "sec_fraction", "zone"] # => ["+", "-", "<<", "<=>", ">>", "_dump", "ajd", "amjd", "asctime", "ctime", "cwday",..., "yday", "year", "zone"]

Tags: technology:coding, technology:ruby Last modified 03:25 Mon, 6 Oct 2008 by AmanKing. Accessed 1,092 times Children What Links Here share Share Except where expressly noted, this work is licensed under a Creative Commons License.