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.
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!