Skip to content

Instantly share code, notes, and snippets.

@thekcsam
Forked from jinwen95/orange_tree.rb
Created November 11, 2015 09:58
Show Gist options
  • Select an option

  • Save thekcsam/6d3de67ee2c9b31b993c to your computer and use it in GitHub Desktop.

Select an option

Save thekcsam/6d3de67ee2c9b31b993c to your computer and use it in GitHub Desktop.

Revisions

  1. @jinwen95 jinwen95 revised this gist Nov 11, 2015. 1 changed file with 78 additions and 6 deletions.
    84 changes: 78 additions & 6 deletions orange_tree.rb
    Original file line number Diff line number Diff line change
    @@ -3,25 +3,97 @@ class NoOrangesError < StandardError
    end

    class OrangeTree

    def initialize
    @age = 0
    @orange_on_tree = []
    @height = 0
    puts "Orange is growing!"
    end

    attr_reader :age, :height

    # Ages the tree one year
    def age!
    @age += 1

    if @age <= 60
    @height += 10
    end

    @orange_on_tree = []



    if @age > 5 && @age < 60
    a = rand(1..10)
    a.times do
    @orange_on_tree << Orange.new
    end
    end

    end

    # Returns +true+ if there are any oranges on the tree, +false+ otherwise
    def dead?
    if @age >= 60
    return true
    else
    false
    end
    end

    # # Returns +true+ if there are any oranges on the tree, +false+ otherwise
    def any_oranges?
    # orange is older than certain age
    # byebug
    if @orange_on_tree == []
    false
    else
    true
    end
    end

    # Returns an Orange if there are any
    # Raises a NoOrangesError otherwise
    # # Returns an Orange if there are any
    # # Raises a NoOrangesError otherwise
    def pick_an_orange!
    raise NoOrangesError, "This tree has no oranges" unless self.any_oranges?

    # orange-picking logic goes here
    @orange_on_tree.shift
    end
    end

    class Orange
    # Initializes a new Orange with diameter +diameter+
    def initialize(diameter)
    attr_reader :diameter
    def initialize
    @diameter = rand(3..5)
    end
    end

    end

    tree = OrangeTree.new

    tree.age! until tree.any_oranges?

    puts "Tree is #{tree.age} years old and #{tree.height} feet tall"
    until tree.dead?
    basket = []

    while tree.any_oranges?
    basket << tree.pick_an_orange!
    end

    sum = 0
    basket.each {|orange| sum = sum + orange.diameter}

    avg_diameter = sum / basket.length
    puts "Year #{tree.age} Report"
    puts "Tree height: #{tree.height} feet"
    puts "Harvest : #{basket.size} oranges with an average diameter of #{avg_diameter} inches"
    puts ""

    tree.age!
    end


    puts "Alas, the tree, she is dead."
  2. code-division created this gist Aug 31, 2014.
    27 changes: 27 additions & 0 deletions orange_tree.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # This is how you define your own custom exception classes
    class NoOrangesError < StandardError
    end

    class OrangeTree
    # Ages the tree one year
    def age!
    end

    # Returns +true+ if there are any oranges on the tree, +false+ otherwise
    def any_oranges?
    end

    # Returns an Orange if there are any
    # Raises a NoOrangesError otherwise
    def pick_an_orange!
    raise NoOrangesError, "This tree has no oranges" unless self.any_oranges?

    # orange-picking logic goes here
    end
    end

    class Orange
    # Initializes a new Orange with diameter +diameter+
    def initialize(diameter)
    end
    end