Created
March 22, 2011 22:51
-
-
Save squeedee/882271 to your computer and use it in GitHub Desktop.
BiScoped DSL
This file contains hidden or 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 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 |
This file contains hidden or 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
$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 | |
This file contains hidden or 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
$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