⇐ Previous | Next ⇒

Tiny Fibonacci in Ruby

Post created 2013-12-19 00:09 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.

comments powered by Disqus