Class: Object

Inherits:
BasicObject
Defined in:
lib/singleton_from.rb

Overview

class A singleton_from :start

def initialize(arg1, arg2, &block) p block.call p “Initialize” p arg1 p arg2 end

def start(&block) p block.call p “start” end end

A.start(“1”, “2”) do puts “Hi, everyone… :-)” end

Class Method Summary collapse

Class Method Details

.singleton_from(*method_names) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/singleton_from.rb', line 25

def singleton_from(*method_names)
  self.class.send(:define_method, :instance) do
      @__instance__ ||= new
  end

  method_names.each do |method_name|
    self.class.send(:define_method, method_name) do |*args, &block|
      self.instance.send(__method__, *args, &block)
    end
  end
end