Last Updated: February 25, 2016
·
427
· chadoh

Ancestors of a Ruby class

When debugging my Ruby apps, I often want to know where a certain method comes from. The class method ancestors does just that. Combined with the instance_methods method, it helps me find the class I need right away:

2.1.4 :001 > a = %w(a b c)
 => ["a", "b", "c"]
2.1.4 :002 > a.class
 => Array
2.1.4 :003 > a.class.ancestors
 => [Array, Enumerable, Object, Kernel, BasicObject]
2.1.4 :004 > Enumerable.instance_methods(false).sort
 => [:all?, :any?, :chunk, :collect, :collect_concat, :count, :cycle, :detect, :drop, :drop_while, :each_cons, :each_entry, :each_slice, :each_with_index, :each_with_object, :entries, :find, :find_all, :find_index, :first, :flat_map, :grep, :group_by, :include?, :inject, :lazy, :map, :max, :max_by, :member?, :min, :min_by, :minmax, :minmax_by, :none?, :one?, :partition, :reduce, :reject, :reverse_each, :select, :slice_before, :sort, :sort_by, :take, :take_while, :to_a, :to_h, :zip]