title: How to create a copy constructor in Ruby
body: <p>I'm new to Ruby and I'm programming a game where there is a class named Player and I'm having issues when trying to copy an object. My code looks something like this:</p>
  
  <pre><code>class Player
  
      attr_accessor :imprisoned
      attr_reader :name
      attr_reader :balance
      attr_accessor :freedom_card
      attr_accessor :current_box
      attr_reader :properties
  
      # Default constructor with no parameters
      def initialize (name = "")
          @name = name
          @imprisoned = false
          @balance = 7500
          @properties = Array.new
          @freedom_card = nil
          @current_box = nil
      end
  
      # Constructor with one parameter (it works)
      def self.nuevo (name)
          self.new(name)
      end
  
      # Copy constructor (doesn't seem to work)
      def self.copia (other_player)
          @name = other_player.name
          @imprisoned = other_player.imprisoned
          @balance = other_player.balance
          @properties = other_player.properties
          @freedom_card = other_player.freedom_card
          @current_box = other_player.current_box
          self
      end
  
      # ...
  end
  </code></pre>
  
  <p>When testing it with this:</p>
  
  <pre><code>player = Player.nuevo ("John")
  puts player.name
  player_2 = Player.copia(player)
  puts player_2.name
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>John
  NoMethodError: undefined method `name' for ModeloQytetet::Player:Class
  </code></pre>
  
  <p>What could be the error? It also fails when using other attributes or methods from the copied object but all works fine in the original one. Thanks in advance and sorry for any English mistakes (not my first language).</p>