Skip to content

Instantly share code, notes, and snippets.

@squeedee
Created March 22, 2011 22:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeedee/882271 to your computer and use it in GitHub Desktop.
Save squeedee/882271 to your computer and use it in GitHub Desktop.
BiScoped DSL
class Farm
attr_accessor :animals
def initialize(&block)
@animals = []
block.arity == 1 ? yield(self) : self.instance_eval(&block)
end
def animal(animal_name)
@animals << animal_name
end
def to_s
@animals.join ","
end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'farm.rb'
myFarm = Farm.new do |f|
f.animal :sheep
f.animal :dog
f.animal :cow
f.animal :cat
end
puts myFarm.to_s
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'farm.rb'
myFarm = Farm.new do
animal :manatee
animal :walrus
animal :platypus
animal :shaved_lemur
end
puts myFarm.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment