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