Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Getting Array of non-ancestral methods in class

I want to create a class that has one method that calls all other methods that are not in the super class.

Is there a way I can use obj.methods to only get the non-ancestral methods? Or is there another way to do it entirely.

Thank you

like image 612
stellard Avatar asked Mar 18 '09 19:03

stellard


4 Answers

The standard instance_methods lets you specify if you want to it to include superclass methods:

class Foo
  def bar
  end
end

Foo.instance_methods(false) # => [:bar]
like image 119
Simon Perepelitsa Avatar answered Nov 12 '22 16:11

Simon Perepelitsa


I'm not sure what you're really trying to do here, nor which methods you mean by "all", but if the question is how you figure out which of a class's instance methods aren't inherited, the combination of .instance_methods and .ancestors can get you that info. Here, using Array as an example class:

Array.instance_methods.sort                                                                         
=> ["&", "*", "+", "-", "<<", "<=>", "==", "===", "=~", "[]", "[]=", "__id__", "__send__", "all?", "any?", "assoc", "at", "class", "clear", "clone", "collect", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "detect", "display", "dup", "each", "each_index", "each_with_index", "empty?", "entries", "eql?", "equal?", "extend", "fetch", "fill", "find", "find_all", "first", "flatten", "flatten!", "freeze", "frozen?", "grep", "hash", "id", "include?", "index", "indexes", "indices", "inject", "insert", "inspect", "instance_eval", "instance_of?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "join", "kind_of?", "last", "length", "map", "map!", "max", "member?", "method", "methods", "min", "nil?", "nitems", "object_id", "pack", "partition", "pop", "private_methods", "protected_methods", "public_methods", "push", "rassoc", "reject", "reject!", "replace", "respond_to?", "reverse", "reverse!", "reverse_each", "rindex", "select", "send", "shift", "singleton_methods", "size", "slice", "slice!", "sort", "sort!", "sort_by", "taint", "tainted?", "to_a", "to_ary", "to_s", "transpose", "type", "uniq", "uniq!", "unshift", "untaint", "values_at", "zip", "|"]

Array.ancestors
=> [Array, Enumerable, Object, Kernel]

Array.instance_methods.sort - Array.ancestors.map {|a| a == Array ? [] : a.instance_methods}.flatten
=> ["&", "*", "+", "-", "<<", "<=>", "[]", "[]=", "assoc", "at", "clear", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "each", "each_index", "empty?", "fetch", "fill", "first", "flatten", "flatten!", "index", "indexes", "indices", "insert", "join", "last", "length", "map!", "nitems", "pack", "pop", "push", "rassoc", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "shift", "size", "slice", "slice!", "sort!", "to_ary", "transpose", "uniq", "uniq!", "unshift", "values_at", "|"]

If you literally only want to rule out methods from the superclass, as opposed to the included ones, there's also .superclass.

Array.superclass
=> Object

Array.instance_methods.sort - Array.superclass.instance_methods
=> ["&", "*", "+", "-", "<<", "<=>", "[]", "[]=", "all?", "any?", "assoc", "at", "clear", "collect", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "detect", "each", "each_index", "each_with_index", "empty?", "entries", "fetch", "fill", "find", "find_all", "first", "flatten", "flatten!", "grep", "include?", "index", "indexes", "indices", "inject", "insert", "join", "last", "length", "map", "map!", "max", "member?", "min", "nitems", "pack", "partition", "pop", "push", "rassoc", "reject", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "select", "shift", "size", "slice", "slice!", "sort", "sort!", "sort_by", "to_ary", "transpose", "uniq", "uniq!", "unshift", "values_at", "zip", "|"]

Does that help?

like image 30
glenn mcdonald Avatar answered Nov 12 '22 16:11

glenn mcdonald


obj1.class.instance_methods - obj1.class.superclass.instance_methods

like image 5
guyinsb Avatar answered Nov 12 '22 15:11

guyinsb


class MassiveCall
  def method1
    puts "calling method1"
  end

  def method2
    puts "calling method2"
  end

  def method3
    puts "calling method3"
  end

  def one_method_to_rule_them_all
    # skip one_method_to_rule_them_all to avoid infinite recursion:
    methods = self.class.instance_methods(false) - ["one_method_to_rule_them_all"]
    methods.each do |method_name|
      self.send(method_name)
    end
  end
end

master = MassiveCall.new
master.one_method_to_rule_them_all
like image 2
Marcin Urbanski Avatar answered Nov 12 '22 16:11

Marcin Urbanski