I'm brand new to ruby and I've run into an error that I haven't been able to find the answer to in google, or stack overflow.
I'm trying to put the first 10 values of the fibinachi sequence into an array like so:
#@fib=[] #Maybe try creating the array differently?
@fib=Array.new
foo=42
puts "foo is of type #{foo.class}"
@fib.push(42) #Testing the array
puts @fib #Show the test
def find_fib(anumber)
return anumber if anumber <= 1
( find_fib(anumber - 1) + find_fib(anumber - 2 ))
#@fib.push(anumber.to_i) #Maybe I need to specify it is an integer http://stackoverflow.com/questions/11466988/ruby-convert-string-to-integer-or-float
puts "anumber is of type #{anumber.class}"
puts "They array is of type #{@fib.class}"
puts "a number is #{anumber}"
@fib.push(anumber) #<= this line fails
end
puts find_fib(10)
I am getting the following error:
...`+': no implicit conversion of Fixnum into Array (TypeError)
.......
foo is of type Fixnum
42
anumber is of type Fixnum
They array is of type Array
a number is 2
[Finished in 0.3s with exit code 1]
Can someone explain to me what is different between foo
and anumber
that prevents me from appending to the array? After all, they are both 'Fixnum' datatypes.