Class: Object

Inherits:
BasicObject
Defined in:
lib/plexus/ext.rb

Instance Method Summary collapse

Instance Method Details

#is_a?(klass) ⇒ Boolean

Check wether the object is of the specified kind. If the receiver has a singleton class, will also perform the check on its singleton class’ ancestors, so as to catch any included modules for object instances.

Example:

class A; include Digraph; end
a.singleton_class.ancestors
# => [Plexus::DirectedGraph::Algorithms, ...
      Plexus::Labels, Enumerable, Object, Plexus, Kernel, BasicObject]
a.is_a? Plexus::Graph
# => true

Parameters:

  • klass (Class)

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/plexus/ext.rb', line 38

def is_a? klass
  sc = self.singleton_class
  if not sc.nil?
    self.singleton_class.ancestors.include?(klass) || super
  else
    super
  end
end

#singleton_classObject

Get the singleton class of the object. Depending on the object which requested its singleton class, a ‘module_eval` or a `class_eval` will be performed. Object of special constant type (`Fixnum`, `NilClass`, `TrueClass`, `FalseClass` and `Symbol`) return `nil` as they do not have a singleton class.

Returns:

  • the singleton class



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/plexus/ext.rb', line 10

def singleton_class
  if self.respond_to? :module_eval
    self.module_eval("class << self; self; end")
  elsif self.respond_to? :instance_eval
    begin
      self.instance_eval("class << self; self; end")
    rescue TypeError
      nil
    end
  end
end