vendredi 22 septembre 2017

Ruby IF/else condensing

This is a part of a Ruby script i've been working on, and want to condense this if statement somehow. How would you make this code shorter or less heavy, maybe using r3[i-=1] to automate in an iteration loop? It is pulling from an array:

r3=[5.0, 10.0, 20.0, 50.0, 70.0, 90.0]

if over > r3[5]
  nx = r3[5]
  puts "Adds #{nx}"
elsif over >= r3[4]
  nx = r3[4]
  puts "Adds #{nx}"      
elsif over >= r3[3] #25
  nx = r3[3]
  puts "Adds #{nx}"
elsif over >= r3[2]
  nx = r3[2] * 2
  puts "Adds #{nx}"
elsif over >= r3[1] * 2 #5lbs
  nx =  r3[1] * 2
  puts "Adds #{nx}"
else
  nx = r3[0]
  puts "Adds #{nx}"
end

Aucun commentaire:

Enregistrer un commentaire