Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created October 23, 2008 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peeja/19070 to your computer and use it in GitHub Desktop.
Save Peeja/19070 to your computer and use it in GitHub Desktop.
# klass.first_responder(:method) gives you the Module in klass's inheritance chain which will respond to :method.
# klass.all_responders(:method) gives you all of the Modules in klass's inheritance chain which can respond to :method, in order from klass to Kernel.
require 'metaid'
class Object
def first_responder(method)
method = method.to_s
metaclass.ancestors.find do |mod|
mod.instance_methods(false).include? method
end
end
def all_responders(method)
method = method.to_s
metaclass.ancestors.find_all do |mod|
mod.instance_methods(false).include? method
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment