class
Warcraft
def initialize &block
instance_eval &block
end
def method_missing name, *args, &block
instance_variable_set("@#{name}".to_sym, args[0])
self.class.send(:define_method, name, proc { instance_variable_get("@#{name}")})
end
def game &block
@game = Game.new &block
end
class Game
def initialize &block
instance_eval &block
end
def method_missing name, *args, &block
instance_variable_set("@#{name}".to_sym, args[0])
self.class.send(:define_method, name, proc { instance_variable_get("@#{name}")})
end
end
end
warcraft =
Warcraft.new do
name "
Warcraft III"
battle_net :iccup
game do
side :sentinels
hero "Furion"
rune_appear_every 2
end
end
puts
warcraft.inspect # => #<Warcraft:0x00000000be3e80 @name="
Warcraft III", @battle_net=:iccup, @game=#<Warcraft::Game:0x000000009c6c38 @side=:sentinels, @hero="Furion", @rune_appear_every=2>>
How to access nested methods?
puts
warcraft.battle_net # => iccup
puts
warcraft.side #=> #<Proc:
[email protected]:9 (lambda)>
puts
warcraft.game #=> dsl.rb:18:in `instance_eval': block not supplied (ArgumentError)
puts
warcraft.game.side #=> dsl.rb:18:in `instance_eval': block not supplied (ArgumentError)