/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# encoding = utf-8 # Lib module ObjectExtensions def execute(&block) Extension.new.execute(&block) end end class Parent @@config = [] end class Extension < Parent def execute(&block) self.instance_eval(&block) end def one; puts @@config[0] end def two; puts @@config[1] end def three; puts @@config[2] end end Object.__send__(:include, ObjectExtensions) class Config < Parent class << self def init yield self end def one @@config << 'one' end def two @@config << 'two' end def three @@config << 'three' end end end # Configurating Config.init do |c| c.one #=> set @@config[0] to 'one' c.two #=> set @@config[1] to 'two' c.three #=> set @@config[2] to 'three' end # Usage execute do one #=> one two #=> two three #=> three end