I am trying to write a simple custom function for fibonacci, But i am getting a error:
My code :
class Fibonacci
def fib(num)
@num = num.to_i
series = Array.new
series[0] = 0
series[1] = 1
for i in 0..series[@num]
series[@num+2] = series[@num] + series[@num+1]
end
return series
end
end
obj = Fibonacci.new
obj.fib(8)
Error :
ruby fibonacci.rb
fibonacci.rb:9:in `fib': bad value for range (ArgumentError)
from fibonacci.rb:19:in `<main>'
to_iandreturnare unnecessary here;series = [0,1]can replace lines 2 to 4 in your method definition; use two spaces for indentation throughout;foris not idiomatic Ruby.