Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/clevic/extensions.rb,
lib/clevic/subclasses.rb
Overview
copied from snippets.dzone.com/posts/show/2992
Class Method Summary collapse
-
.subclasses(direct = false) ⇒ Object
return a list of the subclasses of a class from Matt Williams matthewkwilliams.com/index.php/2008/08/22/rubys-objectspace-subclasses/.
Instance Method Summary collapse
-
#evaluate_path(path_ary) ⇒ Object
recursively calls each entry in path_ary will return nil if any entry in path_ary results in a nil value.
-
#with {|_self| ... } ⇒ Object
pass self to the block and return the results of the block.
Class Method Details
.subclasses(direct = false) ⇒ Object
return a list of the subclasses of a class from Matt Williams matthewkwilliams.com/index.php/2008/08/22/rubys-objectspace-subclasses/
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/clevic/extensions.rb', line 21 def self.subclasses(direct = false) classes = [] if direct ObjectSpace.each_object(Class) do |c| next unless c.superclass == self classes << c end else ObjectSpace.each_object(Class) do |c| next unless c.ancestors.include?(self) and (c != self) classes << c end end classes end |
Instance Method Details
#evaluate_path(path_ary) ⇒ Object
recursively calls each entry in path_ary will return nil if any entry in path_ary results in a nil value.
7 8 9 10 11 |
# File 'lib/clevic/extensions.rb', line 7 def evaluate_path( path_ary ) path_ary.inject( self ) do |value, att| value.nil? ? nil : value.send( att ) end end |
#with {|_self| ... } ⇒ Object
pass self to the block and return the results of the block.
14 15 16 |
# File 'lib/clevic/extensions.rb', line 14 def with( &block ) yield( self ) end |