Created
October 15, 2012 03:29
-
-
Save arn-e/3890679 to your computer and use it in GitHub Desktop.
orange_tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MAX_AGE = 120 | |
| class OrangeTree | |
| def initialize | |
| @height_total_inches = 0 | |
| @age = 0 | |
| @orange_count = 0 | |
| end | |
| def height | |
| if @height_total_inches < 12 | |
| puts "#{@height_inches} inches" | |
| else | |
| puts "#{@height_feet} feet, #{@height_inches} inches" | |
| end | |
| end | |
| def increase_height | |
| @height_total_inches += 4 | |
| @height_feet,@height_inches = @height_total_inches.divmod(12) | |
| end | |
| def one_year_passes | |
| if @age >= MAX_AGE | |
| puts "Tragedy! This is an ex-tree!" | |
| return | |
| end | |
| @age = @age + 1 | |
| increase_height | |
| count_the_oranges | |
| # debug_print_variabless | |
| puts "One more year..." | |
| end | |
| def count_the_oranges | |
| if @age <= 3 | |
| @orange_count = 0 | |
| elsif @age > 3 && @age <= 60 | |
| @orange_count = @age | |
| else | |
| @orange_count = MAX_AGE - @age | |
| end | |
| end | |
| def pick_an_orange | |
| if @orange_count >= 1 | |
| @orange_count = @orange_count - 1 | |
| puts "That was the most delicious orange I've ever tasted!" | |
| else | |
| puts "What, no more oranges? Worthless tree." | |
| end | |
| end | |
| def debug_print_variables | |
| puts "height : #{@height_feet} feet, #{@height_inches} inches, age : #{@age}, oranges : #{@orange_count}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment