Skip to content

Instantly share code, notes, and snippets.

@takuyan
Last active December 13, 2017 04:40
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 takuyan/2b6817408bee7e692414dad9012dad61 to your computer and use it in GitHub Desktop.
Save takuyan/2b6817408bee7e692414dad9012dad61 to your computer and use it in GitHub Desktop.
Objectの子クラスとその子クラスがincludeしているModuleを階層的に表示するナニカ
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
@takuyan
Copy link
Author

takuyan commented Dec 9, 2017

Object
-----
 | Array <Enumerable>
 | Binding 
 | Data 
 |  | StringIO <IO::generic_writable>, <IO::generic_readable>, <Enumerable>
 | Dir <Enumerable>
 | Encoding 
 | Enumerator <Enumerable>
 | Exception 
 |  | SystemExit 
 |  | StandardError 
 |  |  | FiberError 
 |  |  | ZeroDivisionError 
 |  |  | RegexpError 
 |  |  | ThreadError 
 |  |  | IOError 
 |  |  |  | EOFError 
 |  |  | ArgumentError 
 |  |  |  | UncaughtThrowError 
 |  |  | IndexError 
 |  |  |  | StopIteration 
 |  |  |  |  | ClosedQueueError 
 |  |  |  | KeyError 
 |  |  | TypeError 
 |  |  | RangeError 
 |  |  |  | FloatDomainError 
 |  |  | RuntimeError 
 |  |  | EncodingError 
 |  |  | LocalJumpError 
 |  |  | SystemCallError 
 |  |  | NameError <DidYouMean::Correctable>
 |  |  |  | NoMethodError <DidYouMean::Correctable>
 |  | SignalException 
 |  |  | Interrupt 
 |  | ScriptError 
 |  |  | LoadError 
 |  |  | NotImplementedError 
 |  |  | SyntaxError 
 |  | SystemStackError 
 |  | SecurityError 
 |  | NoMemoryError 
 | FalseClass 
 | Fiber 
 | Hash <Enumerable>
 | IO <File::Constants>, <Enumerable>
 |  | File <File::Constants>, <Enumerable>
 | MatchData 
 | Method 
 | Module 
 |  | Class 
 | Monitor <MonitorMixin>
 | NilClass 
 | Numeric <Comparable>
 |  | Integer <Comparable>
 |  | Float <Comparable>
 |  | Rational <Comparable>
 |  | Complex <Comparable>
 |  | Integer <Comparable>
 |  | Integer <Comparable>
 | Proc 
 | Random <Random::Formatter>
 | Range <Enumerable>
 | Regexp 
 | RubyVM 
 | String <Comparable>
 | Struct <Enumerable>
 | Symbol <Comparable>
 | Thread 
 | Thread::ConditionVariable 
 | Thread::Mutex 
 | Thread::Queue 
 |  | Thread::SizedQueue 
 | ThreadGroup 
 | Time <Comparable>
 | TracePoint 
 | TrueClass 
 | UnboundMethod 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment