module Nesting
Public Class Methods
of(mod)
click to toggle source
# File lib/nesting.rb, line 3 def of(mod) if nonmodule?(mod) raise TypeError, 'mod is not a class or module' end level = 0 # Special case Object as it's the namespace of all classes. return level if mod == Object level = if mod.name mod.name.split('::').count else -1 # Probably an anonymous module. end level end
parents(mod)
click to toggle source
# File lib/nesting.rb, line 22 def parents(mod) if nonmodule?(mod) raise TypeError, 'mod is not a class or module' end parents = [] return nil if mod == Object if mod.name mod.name.split('::')[0..-2].inject(Object) do |parent, child| const = parent.const_get(child) parents << const const end parents.reverse! << Object else parents = nil # Probably an anonymous module. end parents end
Private Class Methods
nonmodule?(mod)
click to toggle source
# File lib/nesting.rb, line 47 def nonmodule?(mod) !((mod.class == Module) ^ (mod.class == Class)) end