Return to Snippet

Revision: 42809
at March 10, 2011 23:04 by Shamaoke


Updated Code
# 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

Revision: 42808
at March 10, 2011 23:01 by Shamaoke


Initial Code
# 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
  c.two
  c.three
end

# Usage
execute do
  one
  two
  three
end

Initial URL


Initial Description


Initial Title
Configure some behaviour through a separete configuration class

Initial Tags
class, ruby

Initial Language
Ruby