General Assembly Logo

Ruby Fibonacci Challenge

In this challenge, you will create a Fibonacci sequence generator. It will be enumerable and lazy, like Range and the previously created SteppedRange.

Prerequisites

Instructions

  1. Fork and clone this repository.
  2. Change into the new directory.
  3. Install dependencies.
  4. Create and checkout a new branch to work on.
  5. Fulfill the listed requirements.

Starter code is available in lib/challenge.rb. A pull request is not required, but it is necessary if you want a code review.

You may wish to refer to FAQs related to forking, cloning.

Requirements

  • FibonacciRange is Enumerable.
  • FibonacciRange has one instance variable @stop. It stores no other state on the instance.
  • #each stores no more than three values to do its work: current_fib, previous_fib, and next_fib.
  • FibonacciRange.new takes one required, positional argument: stop
  • FibonacciRange always starts at 0 when creating a new sequence.
  • You should not create a list when FibonacciRange.new is called. Instead, FibonacciRange.new(10).map(&:itself) should return [0, 1, 1, 2, 3, 5, 8]. This is referred to as "lazy evaluation".
  • FibonacciRange.new(10).to_a should return [0, 1, 1, 2, 3, 5, 8]

Pay attention to your linter and fix any syntax errors as they occur. You should run rspec spec to run the included tests. Tests will tell you whether or not you've met these requirements.

Bonus

Write a class, FibonacciNumber, that returns the nth number in the Fibonacci sequence. Requirements:

  • FibonacciNumber stores no state on the instance.
  • FibonacciNumber.new and FibonacciNumber.new(1) should both return the the first Fibonacci number, 0. In this case, we use "first" as mathematicians would use it, whereas programmers would say "zeroth".
  • FibonacciNumber.new(3).to_i returns the third Fibonacci number. Since the Fibonacci sequence is 0, 1, 1, 2, 3, 5..., the third number is 1.

This will be very difficult. You may or may not choose to reuse your FibonacciRange. Try it both ways, starting with using FibonacciRange. You will have to make some interesting decisions about how to use it. Then, try a solution without FibonacciRange, instead using recursion. Make note of the tradeoffs between (pros and cons of) each solution.

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.