我尝试写 DSL
class Warcraft
class << self
def config
unless @instance
yield(self)
end
@instance ||= self
end
attr_accessor :name, :battle_net
def game(&block)
@instance ||= Game.new(&block)
end
end
class Game
class << self
def new
unless @instance
yield
end
@instance ||= self
end
attr_accessor :side, :hero
def rune_appear_every(period)
@rune_appear_every = period
end
end
end
end
Warcraft.config do |war|
war.name = "Warcraft III"
war.battle_net = :iccup
war.game do |game|
game.side = :sentinels
game.hero = "Furion"
game.rune_appear_every 2.minutes
end
end
我得到这样的错误:
dsl.rb:41:in `block (2 levels) in <main>': undefined method `side=' for nil:NilClass (NoMethodError)