Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Forked from acook/introspection.rb
Created September 1, 2013 04:31
Show Gist options
  • Save alexanderscott/6402347 to your computer and use it in GitHub Desktop.
Save alexanderscott/6402347 to your computer and use it in GitHub Desktop.
ruby introspection class
#
# You can do `Object.send :include, Introspection`
# Then you can call `.introspect` on anything.
#
# The `introspect` method returns a hash with information about the receiver
# including its Class, the Constants and instance_methods defined inside it.
#
# The "depth" parameter indicates how deeply it should inspect the object.
# By default there is no depth limit. Pass in 1 to just get info on the object itself.
#
module Introspection
def introspect depth = 0
if self.kind_of?(Module) && ![Object, Module, Class, BasicObject, Kernel].include?(self)
@ancestors = self.ancestors
@constants = []
self.constants.sort.each do |c|
object = self.const_get(c)
if object.respond_to?(:contents) && (depth == 0 || depth > 1) then
@constants << object.contents(depth == 0 ? 0 : depth - 1)
else
@constants << object.introspect_name
end
end
@instance_methods = []
self.instance_methods(false).each do |m|
@instance_methods << self.instance_method(m)
end
end
structure = Hash.new
structure[:class] = self.class
structure[:ancestors] = @ancestors unless @ancestors.nil? || @ancestors.length <= 1
structure[:constants] = @constants unless @constants.nil? || @constants.empty?
structure[:instance_methods] = @instance_methods unless @instance_methods.nil? || @instance_methods.empty?
{self.introspect_name => structure}
end
def introspect_name
if self.kind_of? Module
self.methods(false).include?(:inspect) ? self.name : self
else
self
end
end
end
Object.send :include, Introspection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment