Spawning quick DSLs for fun and profit is super easy with Ruby. This toy is a template for stealing for something useful.

Let’s imagine a hypothetical, and not very useful, mini-DSL of the form:

Blog.post do
title { 'Hello World' }
body { 'Hello from the Bellerophon' }
end

How would we model that in code?

class Blog

def self.post(&block)
blog_post = Blog.new
blog_post.instance_eval(&block) if block_given?
blog_post
end

def title(&block)
@title = block.call(self) if block_given?
@title
end

def body(&block)
@body = block.call(self) if block_given?
@body
end

private
def initialize
@title = nil
@body = nil
end
end