I am doing some exercises to help with understanding how various enumerables work under the hood. To do so I have to solve a problem using .each then solve the same problem with the enum that I'm working on, in this case .sort_by. I have everything figured out with the exception of how to actually compare the cents aka the decimal places in these float numbers.
In my code transformed will be an array of arrays. Each array inside transformed will consist of the actual data being compared along with the price that data came from. Because this is an each iteration, I cannot hardcode the data to compare. Because it's a float instead and not a string or array, I cannot tell my code to compare price[-2..-1] as I might with a string/array. I can't multiply each price by 100 and compare those because that would compare the entire amount, not just the cents as required.
def test_sort_by_number_of_cents
prices = [3.02, 9.91, 7.9, 10.01, 11.0]
transformed = []
prices.each do |price|
transformed << [X, price]
end
transformed = transformed.sort
sorted = []
transformed.each do |sort_key, price|
sorted << price
end
assert_equal [11.0, 10.01, 3.02, 7.9, 9.91], sorted
end
The expected result can be seen in the assertion: [11.0, 10.01, 3.02, 7.9, 9.91]