Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
¿Cómo hacer que 2 entidades de la misma clase interactúen?

Estoy tratando de hacer que 2 "personas" de la misma clase interactúen entre sí y cambien las "estadísticas" de cada uno. Sin embargo, aparece el error "el argumento formal no puede ser una constante".

 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")

Aquí estoy tratando de hacer que el jugador 1 "restaure" al jugador 2 a su maná completo. Sé que el código no es perfecto, pero no estoy seguro de cómo hacerlo. Centrándose en el comando de restauración, los demás funcionan bien.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debería poder pasar la instancia del jugador que desea afectar al método de restore en lugar de la cadena de nombre. Luego puede actualizar los atributos de ese jugador según sea necesario. Un ejemplo rápido:

Actualice la clase para hacer que el mana se pueda escribir:

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

Actualice el método de restore para aceptar la instancia del jugador:

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

Pasar instancia al método:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!