Created
May 26, 2013 07:28
-
-
Save cu39/5651974 to your computer and use it in GitHub Desktop.
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
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