Ao depurar meus aplicativos Ruby, geralmente quero saber de onde vem um determinado método. O método da classeancestors
faz exatamente isso. Combinado com o instance_methods
método , ele me ajuda a encontrar a aula de que preciso imediatamente:
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]