Software Development AgencyVermont.dev
Blog

Tiny Fibonacci in Ruby

12/19/2013

by Gabe Koss


I really enjoy golfing with code; solving a problem in as few characters as possible. Here is my smallest attempt at the classic Fibonacci sequence in Ruby.

def f(s,a=[])
  s.times{a<<(a.size>1?a[-2]+a[-1]:a.size)};a
end

print f(10)
#=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Edit: Whoa, there are some crazy variants I found at Stack Exchange.