Class: Class

Inherits:
Object show all
Defined in:
lib/extensions/class.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


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

#parentsObject

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