removing mixin methods

Discussion in 'Ruby' started by dc, Oct 31, 2008.

  1. dc

    dc Guest

    hi -

    trying to debug some metaprogramming, i just want to find the methods
    of an object, and remove all methods created by mixins from other
    modules.

    its easy enough to remove the superclass/inheritance chain,

    mm = @pet.methods - @pet.class.superclass.methods

    but is there a way to remove any methods that come from other modules
    mixed in?

    thanks!

    /dc
     
    dc, Oct 31, 2008
    #1
    1. Advertising

  2. dc

    Mike Gold Guest

    dc wrote:
    > hi -
    >
    > trying to debug some metaprogramming, i just want to find the methods
    > of an object, and remove all methods created by mixins from other
    > modules.
    >
    > its easy enough to remove the superclass/inheritance chain,
    >
    > mm = @pet.methods - @pet.class.superclass.methods
    >
    > but is there a way to remove any methods that come from other modules
    > mixed in?
    >
    > thanks!
    >
    > /dc


    Does this help?

    class MyArray < Array
    def foo
    end
    end

    origins = MyArray.ancestors.inject(Hash.new) { |result, mod|
    result.update(mod => mod.instance_methods(false))
    }

    require 'pp'
    pp origins

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Mike Gold, Oct 31, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank

    Extract methods in a class to mixin?

    Florian Frank, Sep 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    114
    Florian Frank
    Sep 10, 2003
  2. Michael Roth

    Mixin of class methods?

    Michael Roth, Sep 24, 2005, in forum: Ruby
    Replies:
    8
    Views:
    145
    michele
    Sep 26, 2005
  3. Kenneth McDonald
    Replies:
    5
    Views:
    353
    Kenneth McDonald
    Sep 26, 2008
  4. James Coglan
    Replies:
    4
    Views:
    112
    ara.t.howard
    Nov 15, 2008
  5. John Lane
    Replies:
    6
    Views:
    201
    John Lane
    Feb 9, 2010
Loading...

Share This Page