Skip to content

Instantly share code, notes, and snippets.

@granolocks
Created March 10, 2012 01:16
Show Gist options
  • Save granolocks/2009571 to your computer and use it in GitHub Desktop.
Save granolocks/2009571 to your computer and use it in GitHub Desktop.
Enumerate Constants in object space
class Object
def self.sub_constants(prefix = "", namespace=[])
self.constants.each do |constant|
constant = constant.to_s
puts prefix + constant
#puts eval("#{constant}.methods").inspect
begin
next if %w{ Object Module Class }.include?(constant) || namespace.include?(constant)
b = (namespace.dup) << constant
eval_string = b.join('::')
eval(eval_string).sub_constants(prefix + " ", b)
rescue
next
end
end
end
end
Object.sub_constants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment