Module: MoreCoreExtensions::ClassHierarchy
- Defined in:
- lib/more_core_extensions/core_ext/class/hierarchy.rb
Instance Method Summary collapse
-
#descendant_get(desc_name) ⇒ Object
Returns the descendant with a given name.
-
#hierarchy ⇒ Object
Returns a tree-like Hash structure of all descendants.
-
#leaf_subclasses ⇒ Object
Returns an Array of all descendants which have no subclasses.
-
#lineage ⇒ Object
Returns an Array of all superclasses.
Instance Method Details
#descendant_get(desc_name) ⇒ Object
Returns the descendant with a given name
require 'socket'
IO.descendant_get("IO")
# => IO
IO.descendant_get("BasicSocket")
# => BasicSocket
IO.descendant_get("IPSocket")
# => IPSocket
15 16 17 18 19 20 |
# File 'lib/more_core_extensions/core_ext/class/hierarchy.rb', line 15 def descendant_get(desc_name) return self if desc_name == name || desc_name.nil? klass = descendants.find { |desc| desc.name == desc_name } raise ArgumentError, "#{desc_name} is not a descendant of #{name}" unless klass klass end |
#hierarchy ⇒ Object
Returns a tree-like Hash structure of all descendants.
require 'socket'
IO.hierarchy
# => {BasicSocket=>
# {Socket=>{},
# IPSocket=>{TCPSocket=>{TCPServer=>{}}, UDPSocket=>{}},
# UNIXSocket=>{UNIXServer=>{}}},
# File=>{}}
31 32 33 |
# File 'lib/more_core_extensions/core_ext/class/hierarchy.rb', line 31 def hierarchy subclasses.each_with_object({}) { |k, h| h[k] = k.hierarchy } end |
#leaf_subclasses ⇒ Object
Returns an Array of all descendants which have no subclasses
require 'socket'
BasicSocket.leaf_subclasses
# => [Socket, TCPServer, UDPSocket, UNIXServer]
49 50 51 |
# File 'lib/more_core_extensions/core_ext/class/hierarchy.rb', line 49 def leaf_subclasses descendants.select { |d| d.subclasses.empty? } end |
#lineage ⇒ Object
Returns an Array of all superclasses.
require 'socket'
TCPServer.lineage
# => [TCPSocket, IPSocket, BasicSocket, IO, Object, BasicObject]
40 41 42 |
# File 'lib/more_core_extensions/core_ext/class/hierarchy.rb', line 40 def lineage superclass.nil? ? [] : superclass.lineage.unshift(superclass) end |