Class | Class |
In: |
lib/mack-facets/extensions/class.rb
|
Parent: | Object |
Returns a new Object instance of the class name specified.
Examples:
Class.new_instance_of("Orange") => #<Orange:0x376c0c> Class.new_instance_of("Animals::Dog") => #<Animals::Dog:0x376a2c>
# File lib/mack-facets/extensions/class.rb, line 8 8: def self.new_instance_of(klass_name) 9: klass_name.constantize.new 10: end
This will through the ancestor tree of object and tell you if that object is of the specified type.
# File lib/mack-facets/extensions/class.rb, line 14 14: def class_is_a?(klass_name) 15: self.ancestors.each do |an| 16: if an == klass_name 17: return true 18: end 19: end 20: return false 21: end
Returns an array of the classes parent classes.
Examples:
Orange.parents # => [Citrus, Fruit, Object] Citrus.parents # => [Fruit, Object] Fruit.parents # => [Object]
# File lib/mack-facets/extensions/class.rb, line 29 29: def parents 30: ans = [self.superclass] 31: until ans.last.superclass.nil? 32: ans << ans.last.superclass 33: end 34: ans 35: end