Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda