Fibonacci in Ruby

Published on 14 October 2018 (Updated: 07 May 2026)

Welcome to the Fibonacci in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

def usage!
  abort("Usage: please input the count of fibonacci numbers to output")
end

def fibonacci(n)
  a, b = 1, 1

  n.times do |i|
    puts "#{i + 1}: #{a}"
    a, b = b, a + b
  end
end

input = ARGV.first
usage! if input.nil? || input.strip.empty?

begin
  num = Integer(input)
rescue ArgumentError
  usage!
end

usage! if num.negative?

fibonacci(num)

Fibonacci in Ruby was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.