Sunday, 29 December 2013

config with instance_eval

class Application
attr_reader :router
def initialize
@router = Router.new
end
end
class Router
def initialize
@arr = []
end
attr_reader :arr
def config &block
instance_eval &block
end
def add_more a
arr << a
end
end
myApp = Application.new
myApp.router.config do
add_more "ena"
add_more "dio"
end
p myApp.router.arr
#["ena", "dio"]
view raw gistfile1.rb hosted with ❤ by GitHub

No comments:

Post a Comment