Module: Doodle::Singleton

Included in:
BaseMethods
Defined in:
lib/doodle/singleton.rb

Overview

provides more direct access to the singleton class and a way to treat singletons, Modules and Classes equally in a meta context

Instance Method Summary collapse

Instance Method Details

#sc_eval(*args, &block) ⇒ Object

evaluate in class context of self, whether Class, Module or singleton



13
14
15
16
17
18
19
20
# File 'lib/doodle/singleton.rb', line 13

def sc_eval(*args, &block)
  if self.kind_of?(Module)
    klass = self
  else
    klass = self.singleton_class
  end
  klass.module_eval(*args, &block)
end

#singleton_class(&block) ⇒ Object

return the ‘singleton class’ of an object, optionally executing a block argument in the (module/class) context of that object



7
8
9
10
11
# File 'lib/doodle/singleton.rb', line 7

def singleton_class(&block)
  sc = class << self; self; end
  sc.module_eval(&block) if block_given?
  sc
end