Skip to content

Instantly share code, notes, and snippets.

@markburns
Last active December 25, 2016 07:15
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 markburns/9c4ac9495ee56a77de16983d6e83d49e to your computer and use it in GitHub Desktop.
Save markburns/9c4ac9495ee56a77de16983d6e83d49e to your computer and use it in GitHub Desktop.
If you ever use class instance variables, you'll run into state hell. This recursively searches a namespace for any instance variables and prints them out. Skipping circular constant definitions
def show_class_instance_variables(n)
puts "#{n} #{n.instance_variables}" if n.instance_variables.length.positive?
return unless (n.is_a?(Class) || n.is_a?(Module))
n.constants.each do |c|
next if n==c
klass = n.const_get(c)
if klass.is_a?(Class) || klass.is_a?(Module)
puts "#{klass} #{klass.instance_variables}" if klass.instance_variables.length.positive?
klass.constants.each do |s|
s = klass.const_get(s)
next if s == klass || s == n
show_class_instance_variables(s)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment