Skip to content

Instantly share code, notes, and snippets.

@cu39
Created May 26, 2013 07:28
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 cu39/5651974 to your computer and use it in GitHub Desktop.
Save cu39/5651974 to your computer and use it in GitHub Desktop.
class BlockConf
CONF_NAMES = [:id, :pw]
CONF_NAMES.each do |name|
define_method name { |v| @conf[name] = v }
end
def self.configure &blk
self.new &blk
end
attr_reader :conf
def initialize opts={}, &blk
@conf = opts
instance_eval &blk unless blk.nil?
end
end
bc = BlockConf.configure do
id 'foooo'
pw 'barrr'
end
puts bc.conf[:id] #=> foooo
puts bc.conf[:pw] #=> barrr
bc2 = BlockConf.new id:'barrr', pw:'bazzz'
puts bc2.conf[:id] #=> barrr
puts bc2.conf[:pw] #=> bazzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment