Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Created September 11, 2010 03:29
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 dominictarr/574754 to your computer and use it in GitHub Desktop.
Save dominictarr/574754 to your computer and use it in GitHub Desktop.
ruby DSL example
class DSL
def go (&block)
instance_eval &block if block
self
end
def self.go (&block)
DSL.new.go(&block)
end
def from (old_self,&block)
@old_self = old_self
instance_eval &block if block
self
end
def method_missing(method,*args,&block)
if @old_self then
return @old_self.method(method).call(*args,&block)
else
raise NoMethodError.new "could not find #{method} on #{self} or #{@old_self}"
end
end
def x message
puts "#{self.class}.X: #{message}"
end
end
def x message
puts "X: #{message}"
end
def y message
puts "Y: #{message}"
end
x "outside dsl: #{self.class}"
DSL.go {
x "inside dsl: #{self.class}"
y "inside dsl: #{self.class}"
}
class Foo
def x message
puts "Foo.X: #{message}"
end
def y message
puts "Foo.Y: #{message}"
end
def z message
puts "Foo.Z: #{message}"
end
def useDsl
DSL.go.from(self) {
x "class dsl #{self.class}"
y "class dsl: #{self.class}"
z "class dsl: #{self.class}"
}
begin
DSL.go {
z "class dsl: #{self.class}"
}
rescue Exception => e
puts " #{e.class} - #{e}"
end
end
end
Foo.new.useDsl
@dominictarr
Copy link
Author

output of this gist here: run this gist

@drawgrunt
Copy link

Your article is very appealing, with clear instructions. slope game

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