-
-
Save havenwood/d74b31d1e3fdcc56768a03c3480ffbc4 to your computer and use it in GitHub Desktop.
Example Singleton for #ruby IRC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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