Last active
December 13, 2017 04:40
-
-
Save takuyan/2b6817408bee7e692414dad9012dad61 to your computer and use it in GitHub Desktop.
Objectの子クラスとその子クラスがincludeしているModuleを階層的に表示するナニカ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prefix(depth=1) | |
s = ' | ' | |
s * depth | |
end | |
def included_modules(klass, hash) | |
modules = Object.const_get(klass).included_modules | |
modules.delete(Kernel) | |
if modules | |
modules.map do |c| | |
%(<#{c}>) | |
end.join(', ') | |
end | |
end | |
def ptree(klass, hash, depth) | |
puts %(#{prefix(depth)}#{klass} #{included_modules(klass, hash)}) | |
if hash[klass] | |
hash[klass].each do |c| | |
ptree(c, hash, depth + 1) | |
end | |
end | |
end | |
hash = Object.constants.select do |c| | |
# NOTE: ModuleじゃなくてClassを集める | |
Object.const_get(c).class == Class | |
end.compact.map do |c| | |
# NOTE: 親クラスと子クラスのarrayをつくる | |
[Object.const_get(c).superclass.to_s, Object.const_get(c).to_s] | |
end.reduce({}) do |a, pair| | |
# NOTE: 親クラスをキーとして値に子クラスを配列を詰める | |
a[pair.first] ||= [] | |
a[pair.first] << pair.last | |
a | |
end | |
puts 'Object' | |
puts '------' | |
hash['Object'].sort.each do |c| | |
ptree(c, hash, 1) | |
end |
Author
takuyan
commented
Dec 9, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment