title: Ruby Beginner - Accessing Another Class Object
body: <p>I'm trying to create a simple turn-based battle in Ruby, but I keep getting stuck when it comes to classes. I tried to do this by starting with basic code and building it up around that. I was able to make it work simply enough by using regular variables and basic attack code:</p>
  
  <pre><code>player = "goodguy"
  player_health = 15
  player_damage = 5
  
  enemy = "badguy"
  enemy_health = 15
  enemy_damage = 5
  
  puts "#{player} attacks #{enemy} and does #{player_damage} damage."
  enemy_health -= player_damage
  puts "#{enemy} has #{enemy_health} remaining."
  </code></pre>
  
  <p>Then, I turned the attack into a function (I had to make the variables global so the function could see them):</p>
  
  <pre><code>$player = "goodguy"
  $player_health = 15
  $player_damage = 5
  
  $enemy = "badguy"
  $enemy_health = 15
  $enemy_damage = 5
  
  def player_attack
    puts "#{$player} attacks #{$enemy} and does #{$player_damage} damage."
    $enemy_health -= $player_damage
    puts "#{$enemy} has #{$enemy_health} health remaining."
    if $enemy_health &lt;= 0
      puts "#{$enemy} died!"
    end    
  end    
  
  player_attack()
  </code></pre>
  
  <p>Next, I turned <code>Player</code> into a class:</p>
  
  <pre><code>class Player
    attr_accessor :name; :hp; :damage
    def initialize(name, hp, damage)
      @name = name
      @hp = hp
      @damage = damage
    end
  
    def attack
      puts "#{self.name} attacks #{$enemy}!"
      $enemy_health -= @damage
      puts $enemy_health
    end
  end
  
  $enemy = "badguy"
  $enemy_health = 15
  $enemy_damage = 5    
  
  me = Player.new("goodguy", 15, 5)
  
  me.attack
  </code></pre>
  
  <p>This is where I get stuck. When I turn <code>Enemy</code> into a class (basically modeled exactly after the <code>Player</code> class), I can't figure out how to make the two objects interact with each other. This code doesn't work, but here's the last of what I tried. The <code>#{}</code> variables are more to show what I'm trying to make happen than anything else: </p>
  
  <pre><code>class Player
    attr_accessor :name; :hp; :damage
    def initialize(name, hp, damage)
      @name = name
      @hp = hp
      @damage = damage
    end
  
    def attack
      puts "#{self.name} attacks #{badguy.name}!"
      badguy.hp -= @damage
      puts badguy.hp
    end
  end
  
  class Enemy
    attr_accessor :name; :hp; :damage
    def initialize(name, hp, damage)
      @name = name
      @hp = hp
      @damage = damage
    end
  
    def attack
      puts "#{self.name} attacks #{goodguy.name}!"
      player.hp -= @damage
      puts player.hp
    end
  end  
  
  goodguy = Player.new("Nicehero", 15, 5)
  badguy = Enemy.new("Eviljerk", 15, 5)
  
  me.attack 
  </code></pre>
  
  <p>Basically, what I want to do is make it so that the <code>Player</code> object can interact with the <code>Enemy</code> object. I can't seem to get this working when I try to make 2 classes interact with each other; Also, the #{variable.name} isn't the only thing I've tried for getting the functions to report those values, but I can't seem to find how to actually reference that object. </p>
  
  <p>Obviously there's something I'm missing about how objects interact or what my code is doing vs what I think it should be doing. I would appreciate any suggestions on getting these two classes to interact or how this should be rewritten so that it functions as intended. </p>