while
def 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
until
def 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
loop
def 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
As you have probably noticed, Ruby has no increment operator (i++
) like some other languages do. Instead, constructs like i += 1
(which is equal to i = i + 1
) can be used.