whiledef self.years_before_desired_balance(current_balance, desired_balance)
years = 0
while current_balance < desired_balance
current_balance = annual_balance_update(current_balance)
years += 1
end
years
end
untildef self.years_before_desired_balance(current_balance, desired_balance)
years = 0
until current_balance >= desired_balance
current_balance = annual_balance_update(current_balance)
years += 1
end
years
end
loopdef self.years_before_desired_balance(current_balance, desired_balance)
years = 0
loop do
current_balance = annual_balance_update(current_balance)
years += 1
return years if current_balance >= desired_balance
end
end
Come avrai probabilmente notato, Ruby non ha un operatore di incremento (i++) come invece hanno alcuni altri linguaggi. Al suo posto puoi usare costrutti come i += 1 (che equivale a i = i + 1).