Created
October 8, 2010 14:25
-
-
Save xenda/616883 to your computer and use it in GitHub Desktop.
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
| module Zerg | |
| class Zergling | |
| def attack(victim) | |
| puts "Zerg attacks: Hsssssss" | |
| victim.die if rand(10) > 6 | |
| end | |
| def die | |
| puts "Zerg dies: Hiiiisssssshhh Haaaashhppp" | |
| end | |
| end | |
| class Hive | |
| @@larva = rand(5) | |
| def hatch(zerg) | |
| if @@larva > 0 | |
| @@larva -= rand(5) | |
| zerg.new | |
| end | |
| end | |
| def method_missing(method,*args) | |
| # Si el metodo empieza como hatch_ALGO | |
| if method.to_s =~ /^hatch_(.*)/ | |
| # klass tiene el nombre ahora, ej: "zergling" | |
| klass = $1 | |
| # la primera linea mayuscula: "Zergling" | |
| klass.gsub!(/\b\w/){$&.upcase} | |
| # obtenemos la constante con namespace Zerg: Zerg:Zergling | |
| klass = Object.const_get("Zerg")::const_get(klass) | |
| # pedimos ese codigo | |
| hatch(klass) | |
| else | |
| super | |
| end | |
| end | |
| end | |
| end | |
| module Terran | |
| class CommandCenter | |
| def train(unit,name) | |
| unit.new(name) | |
| end | |
| end | |
| class Marine | |
| attr_accessor :name | |
| def initialize(name) | |
| @name = name | |
| end | |
| def attack(zerg) | |
| puts "Marine attacks: PEW PEW PEW" | |
| zerg.die if rand(10) > 5 | |
| end | |
| def die | |
| puts "Marine dies: NOOoooohoho" | |
| end | |
| end | |
| end | |
| # Construimos un hive | |
| hive = Zerg::Hive.new | |
| zergling = hive.hatch(Zerg::Zergling) | |
| #Construimos un Command Center | |
| nuclear_cc = Terran::CommandCenter.new | |
| marine = nuclear_cc.train(Terran::Marine, "Jim Raynor") | |
| if zergling | |
| marine.attack(zergling) | |
| zergling.attack(marine) | |
| else | |
| puts "#{marine.name} is so alone right now. Where is Kerrigan you ask?" | |
| end | |
| another_zergling = hive.hatch_zergling | |
| rush = hive.hatch_zergling |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment