Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created November 5, 2008 13:41
Show Gist options
  • Save jcoglan/22334 to your computer and use it in GitHub Desktop.
Save jcoglan/22334 to your computer and use it in GitHub Desktop.
class Module
# Inspect the method lookup process like you can in JS.Class
# http://jsclass.jcoglan.com/reflection.html
#
# Array.lookup :any?
# #=> [#<UnboundMethod: Enumerable#any?>]
#
# class Array; def any?; super; end; end
# Array.lookup :any?
# #=> [#<UnboundMethod: Array#any?>, #<UnboundMethod: Enumerable#any?>]
#
def lookup(name)
name = name.to_sym
ancestors.map { |mod|
mod.instance_methods(false).map { |method_name|
method_name.to_sym
}.include?(name) ?
mod.instance_method(name) :
nil
}.compact
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment