Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 3, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/d74b31d1e3fdcc56768a03c3480ffbc4 to your computer and use it in GitHub Desktop.
Save havenwood/d74b31d1e3fdcc56768a03c3480ffbc4 to your computer and use it in GitHub Desktop.
Example Singleton for #ruby IRC
require 'singleton'
module Base
attr_reader :properties
def self.included(base)
base.instance.instance_variable_set(:@properties, [])
base.instance_eval do
def property(name)
instance.properties << name
end
end
end
end
class Sub
include Singleton
include Base
property :foo
end
Sub.instance.properties
#=> [:foo]
module Base
attr_reader :properties
def property(name)
@properties << name
end
def self.extended(base)
base.instance_eval do
@properties = []
end
end
end
class Sub
extend Base
property :foo
end
p Sub.properties
#=> [:foo]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment