Take a look at this code please :
fighter = [:str => 2, :dex => 3, :mag => 3, :acc => 2, :hp => 2]
ranger = [:str => 3, :dex => 2, :mag => 3, :acc => 2, :hp => 2]
magician = [:str => 3, :dex => 3, :mag => 2, :acc => 2, :hp => 2]
rate = Hash.new
if user.first_class == 'Fighter' then rate = fighter end
if user.first_class == 'Ranger' then rate = magician end
if user.first_class == 'Magician' then rate = ranger end
cost = Hash.new
cost[:str] = (user.strength_points + 1) * rate[:str]
cost[:dex] = (user.dexterity_points + 1) * rate[:dex]
cost[:mag] = (user.magic_points + 1) * rate[:mag]
cost[:acc] = (user.accuracy_points + 1) * rate[:acc]
cost[:hp] = (user.health_points + 1) * rate[:hp]
cost
This reside in a function i’ve made, and i get a “Symbol as array index” when i execute it in Rails. I’m guessing that this is because of rate = fighter, rate = magician or rate = ranger possibility. Maybe i have to use clone on that.
My question is, what is the better way of selecting a hash based on an if comparison, doing the above thing ?
You are creating an
Arraywith one entry which is aHash:What you meant to do is just create a
Hash:Also, instead of three
ifstatements, use acasestatement: