1.

Write a single line of Ruby code that prints the Fibonacci sequence of any length as an array.

Answer»

There are multiple ways to do this, but one possible ANSWER is: 

(1..20).INJECT( [0, 1] ) { | fib | fib << fib.last(2).inject(:+) }

As you GO up the sequence fib, you sum, or inject(:+), the last two elements in the array and add the result to the end of fib. 

Note: inject is an ALIAS of reduce 



Discussion

No Comment Found