Skip to content

Instantly share code, notes, and snippets.

@tempredirect
Created April 7, 2014 08:00
Show Gist options
  • Save tempredirect/10016361 to your computer and use it in GitHub Desktop.
Save tempredirect/10016361 to your computer and use it in GitHub Desktop.
HashBuilder
class HashBuilder
def initialize
@h = Hash.new
end
def set(key, value = nil, &block)
@h[key.to_s] = if block_given?
HashBuilder.build &block
else
value
end
end
def self.build(&block)
builder = HashBuilder.new
if block_given?
builder.instance_eval &block
end
@h
end
def method_missing(symbol, *args)
set(symbol, *args)
end
end
hb = HashBuilder.new
r = hb.build {
wibble 42
wobble 43
wiggle {
set "a", 1
set "b", 2
}
banana 25
set "big-thing", 42
}
puts r.inspect
@thommay
Copy link

thommay commented Apr 7, 2014

think we can get to:

default "xmlapi" do
foo "bar"
end

shared_service "blah" do
baz "cow"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment