title: how to do polymorphism in Ruby
body: <p>So here is a more java-esq approach (I think)...I have this code in Ruby</p>
  
  <pre><code>class Human
    def move
      p 'Human move: X'
    end
  end
  
  
  class Computer
    def move
      p'Computer move: O'
    end
  end
  
  class Player
    def initialize(letter)
      if letter == 'X'
        return Human.new
      else
        return Computer.new
      end
    end
  end
  
  bob = Player.new('X')
  bob.move
  </code></pre>
  
  <p>I'm expecting to get back on cli 'Human move: X'
  I'm getting an error of the following...</p>
  
  <pre><code>undefined method `move' for #&lt;Player:0x007f8d930895a8&gt; (NoMethodError)
  </code></pre>
  
  <p>Can anybody help this Ruby noob?</p>
  
  <p>-------------------------discussion------------------------</p>
  
  <p>Now I've just figured a way of doing this in a more, I guess, 'classic' Ruby fashion using inheritance and overriding. Now, this just makes no sense to the sub processors in my mind.I mean, if I do it this way...I could have just required the class files in any other class file and instantiated a new object their. It does not feel truly polymorphic the way it does when you do it in Java....here's the code....</p>
  
  <pre><code>class Player
    def move
      return ""
    end
  end
  
  class Human &lt; Player
    def move
      return 'Human move: X'
    end
  end
  
  
  class Computer &lt; Player
    def move
      return'Computer move: O'
    end
  end
  
  
  
  players = [Human.new, Computer.new]
  players.each {|player|
    print player.move
  }
  </code></pre>