Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created December 14, 2008 06:28
Show Gist options
  • Save Sixeight/35641 to your computer and use it in GitHub Desktop.
Save Sixeight/35641 to your computer and use it in GitHub Desktop.
require 'forwardable'
# A singleton class inspired by Primer class in library
class Class
# We need that method like this become a bultin
def metaclass; class << self; self end end
end
class Foo
class << self
extend Forwardable
_instance = Foo.new
define_method(:instance) { _instance }
# Primer class's cool technic with forwardable
def method_added(meth)
self.metaclass.def_delegator :instance, meth
end
private :new
end
def say
puts 'hello'
end
end
Foo.say #=> hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment