Class: Object

Inherits:
BasicObject
Defined in:
lib/hyperloop/console/object_space.rb

Class Method Summary collapse

Class Method Details

.instanceObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hyperloop/console/object_space.rb', line 2

def self.instance
  ObjectSpace.objects(self).tap do |arr|
    arr.define_singleton_method(:console) { |*args| method_missing(:console, *args) }
    klass = self
    arr.define_singleton_method(:method_missing) do |method, *args, &block|
      if length > 1 && self[0].respond_to?(method)
        raise "Ambiguous #{klass}.instance.#{method} application.\n"\
        "There are #{length} instances of #{klass}.\n"\
        "You can either apply #{method} to a specific instance using #{klass}.instance[x].#{method};\n"\
        "or use an enumerator method like each or collect."
      elsif length == 0
        raise "There are no #{klass} instances."
      else
        self[0].send(method, *args, &block)
      end
    end
  end
end