Skip to content

Instantly share code, notes, and snippets.

@melborne
Created September 30, 2010 02:38
Show Gist options
  • Save melborne/603929 to your computer and use it in GitHub Desktop.
Save melborne/603929 to your computer and use it in GitHub Desktop.
Ruby Class Tree
#!/usr/bin/env ruby -wKU
def class_tree(klass, mod=false)
klass = klass.class unless klass.is_a?(Class)
supers = klass.ancestors.reject { |anc| anc.class == Module unless mod }
method_list = supers.inject({}) do |h, _class|
h[_class] = _class.public_instance_methods(false).
sort.partition { |m| m !~ /^\w/ }.flatten
h
end
method_list.each do |_class, meths|
puts _class
meths = meths.each_slice(5)
meths.each_with_index do |line, i|
mark = i == meths.to_a.length-1 ? "↓" : "|"
puts "#{mark} #{line.join(',')}"
end
end
end
if __FILE__ == $0
class Hello < String
def say
end
end
helo = Hello.new
class_tree(helo, true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment