ruby - A More elegant way of doing this? -
i keep saying myself there must better way can't see right now.. ideas?
i = 0; lose = 0; win = 0 while < @array.size results = @array[i].results q = 0 while q < results.size if results[q].to_i == 0 lose += 1 elsif results[q].to_i == 1 win += 1 else puts results[q] puts "false" end q += 1 end i+=1 end if win == lose puts "true" else puts "false" end
you can use array.each
instead of while loops.
you can use array.count
instead of manually inspecting each array:
lose = results.count { |r| r.to_i == 0 } win = results.count { |r| r.to_i == 1 } # or possibly if array can contain wins , losses win = results.count - lose
Comments
Post a Comment