Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch13 - orange_tree.rb #170

Closed
emmabeynon opened this issue Nov 11, 2015 · 1 comment
Closed

Ch13 - orange_tree.rb #170

emmabeynon opened this issue Nov 11, 2015 · 1 comment

Comments

@emmabeynon
Copy link

Hi guys,

I'm having a lot of trouble getting this one to pass. I even tried using the solution code as-is and it still doesn't work. Here's the error:

Emmas-MacBook-Air:learn_to_program emmabeynon$ rspec spec/ch13/orange_tree_spec.rb

OrangeTree
  reports height and number of oranges (FAILED - 1)

Failures:

  1) OrangeTree reports height and number of oranges
     Failure/Error: expect(ot.one_year_passes).to eq 'This year your tree grew to 10.0m tall, and produced 125 oranges.'

       expected: "This year your tree grew to 10.0m tall, and produced 125 oranges."
            got: "Oh, no! The tree is too old, and has died. :("

       (compared using ==)
     # /Users/emmabeynon/Dropbox (Personal)/Makers_Academy/Projects/learn_to_program/spec/ch13/orange_tree_spec.rb:16:in `block (2 levels) in <top (required)>'

Finished in 0.01465 seconds (files took 0.19631 seconds to load)
1 example, 1 failure

Failed examples:

rspec /Users/emmabeynon/Dropbox (Personal)/Makers_Academy/Projects/learn_to_program/spec/ch13/orange_tree_spec.rb:12 # OrangeTree reports height and number of oranges

@emmabeynon
Copy link
Author

And here's the solution code:

# note we have added a rounding operation on the height to ensure
# the output is sensible in terms of decimal places

class OrangeTree
  def initialize
    @height = 0
    @orange_count = 0
    @alive = true
  end

  def height
    if @alive
      @height.round(1)
    else
      'A dead tree is not very tall. :('
    end
  end

  def count_the_oranges
    if @alive
      @orange_count
    else
      'A dead tree has no oranges. :('
    end
  end

  def one_year_passes
    if @alive
      @height = @height + 0.4
      @orange_count = 0 # old oranges fall off
      if @height > 10 && rand(2) > 0
        # tree dies
        @alive = false
        'Oh, no! The tree is too old, and has died. :('
      elsif @height > 2
        # new oranges grow
        @orange_count = (@height * 15 - 25).to_i
        "This year your tree grew to #{@height.round(1)}m tall," +
          " and produced #{@orange_count} oranges."
      else
        "This year your tree grew to #{@height.round(1)}m tall," +
          " but is still too young to bear fruit."
      end
    else
      'A year later, the tree is still dead. :('
    end
  end

  def pick_an_orange
    if @alive
      if @orange_count > 0
        @orange_count = @orange_count - 1
        'You pick a juicy, delicious orange!'
      else
        'You search every branch, but find no oranges.'
      end
    else
      'A dead tree has nothing to pick. :('
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant