Skip to content

Instantly share code, notes, and snippets.

@rebo
Created December 17, 2012 15:39
Show Gist options
  • Save rebo/4319221 to your computer and use it in GitHub Desktop.
Save rebo/4319221 to your computer and use it in GitHub Desktop.
require 'alias_dci'
class Battle
include AliasDCI::Context
role :lion do
def fight
p "#{name}: RAWR, I am a lion and I run fast."
end
end
role :bear do
def fight
p "#{name}: ARRR, I am a mighty bear and I will eat you."
end
end
def initialize(lion_player, bear_player)
rebind(lion_player, bear_player)
end
def rebind(lion_player, bear_player)
assign_named_roles(:lion => lion_player, :bear => bear_player)
end
def do_battle
in_context do
lion.fight
bear.fight
end
end
end
class Player < Struct.new(:name)
include AliasDCI::DataObject
def fight
puts "#{name}: original fight method"
end
end
#Round 1
player_a = Player.new("Arthur Dent")
player_b = Player.new("Zaphod")
player_a.fight
player_b.fight
battle = Battle.new(player_a, player_b)
#round1
p "ROUND 1 : FIGHT"
battle.do_battle
#Round2
p "ROUND 2 : FIGHT"
battle.rebind(player_b, player_a)
battle.do_battle
player_a.fight
player_b.fight
# >> Arthur Dent: original fight method
# >> Zaphod: original fight method
# >> "ROUND 1 : FIGHT"
# >> "Arthur Dent: RAWR, I am a lion and I run fast."
# >> "Zaphod: ARRR, I am a mighty bear and I will eat you."
# >> "ROUND 2 : FIGHT"
# >> "Zaphod: RAWR, I am a lion and I run fast."
# >> "Arthur Dent: ARRR, I am a mighty bear and I will eat you."
# >> Arthur Dent: original fight method
# >> Zaphod: original fight method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment