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
همانطور که احتمالاً متوجه شدهاید، Ruby برخلاف برخی زبانهای دیگر «عملگر افزایشی» (i++) ندارد. بهجای آن میتوان از ساختارهایی مثل i += 1 (که معادل i = i + 1 است) استفاده کرد.