Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
How to make 2 entities of the same class interact?

I am trying to get 2 "people" of the same class to interact with each other, and change each other's "stats." However, I get the error, "formal argument cannot be a constant."

class Hands 
  attr_reader :name, :element, :skill, :mana, :health, :attack, :fire, :water, :lyfe

  def initialize(name, element, skill)
    @mana = 100
    @health = 200
    @name = name
    @element = element
    @skill = skill
  end

  def mana 
   sleep 1
    puts "#{@name} has #{@mana} mana."
  end

  def health
    sleep 1
    puts "#{@name} has #{@health} HP."
  end

  def restore(Hands)
    if @element == "Lyfe"
      @mana = 100
      puts "#{@name} has been restored!"
    else 
      puts "#{@name} cannot use this ability!"
    end
  end
end

player1 = Hands.new('Silk', 'Lyfe', 'Summon')
player2 = Hands.new('Nubz', 'Lyfe', 'Manipulate Wildlife')
player3 = Hands.new('Lisk', 'Water', 'Invisible')
player4 = Hands.new('Azzi', 'Water', 'Manipulate Water')
player5 = Hands.new('Zeph', 'Fire', 'Lightning')
player6 = Hands.new('Ford', 'Fire', 'Manipulate Fire')
player7 = Hands.new('Boyd', 'Fire', 'Craft')

player1.restore("Nubz")

Here I am trying to get player1 to "restore" player2 back to full mana. I know the code isn't perfect, but I'm not sure how else to do this. Focussing on the restore command, the others work fine.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should be able to pass the instance of the player you want affected to the restore method rather than the name string. You can then update that player's attributes as needed. A quick example:

Update class to make mana writeable:

class Hands 
  attr_reader :name, :element, :skill, :mana, :health, :attack, :fire, :water, :lyfe
  attr_writer :mana

  # ...
end

Update restore method to accept player instance:

def restore(hands)
  if element == "Lyfe"
    hands.mana = 100
    puts "#{hands.name} has been restored!"
  else 
    puts "#{name} cannot use this ability!"
  end
end

Pass instance to method:

player1 = Hands.new('Silk', 'Lyfe', 'Summon')
player2 = Hands.new('Nubz', 'Lyfe', 'Manipulate Wildlife')

player1.restore(player2)
#=> Nubz has been restored!
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda