1

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.

4
  • 1
    By the way, it's the convention in Ruby to use 2-space indent, not 4-space indent.
    – user456814
    Mar 31, 2014 at 3:12
  • @fib.push(number) #<= this line fails looks like a typo. It should be @fib.push(anumber).
    – johnsyweb
    Mar 31, 2014 at 4:11
  • Johnsyweb Thanks for pointing that out. The typo only exists on SO, not on my local copy. I fixed it in the question.
    – spuder
    Mar 31, 2014 at 5:01
  • @spuder by the way, I think one of the problems with your original algorithm might be that each recursive call returns an array that represents the additive operands for a Fibonacci number, instead of just the summed Fibonacci number for that sub-sequence. That's just a hunch though, I'm not 100% sure that that's the problem.
    – user456814
    Apr 1, 2014 at 23:09

2 Answers 2

2

For the error you posted, that's because the terminate condition of the find_fib method returns anumber, which is of type Fixnum. This return value is used in your former recursion:

( find_fib(anumber - 1) + find_fib(anumber - 2 ))

Here you are going to call Array + Fixnum, which causes the type check error. Change the terminate condition to the following may remove that error.

def find_fib(anumber)
  return [anumber] if anumber <= 1
  ...

BTW, you find_fib won't work as expected, you may need further tweak on the algorithm implementation.

1
  • You are right on both accounts, changing anumber to [anumber] got rid of the error. There still is something wrong with the algorithm that I can look into now. gist.github.com/spuder/9885656
    – spuder
    Mar 31, 2014 at 5:05
1

There are a number of problems with your method:

def find_fib(anumber)
  return anumber if anumber <= 1
  (find_fib(anumber - 1) + find_fib(anumber - 2)) # 1
  # ETC...
  @fib.push(number) # 2 and 3
end
  1. You're calculating a Fibonacci number here, but you don't assign the value to a variable, so you're basically throwing away the number.

  2. The last statement evaluated in a Ruby function gets returned, unless you make an explicit return statement like in your first line. As Arie Shaw points out, that last line returns an array object, while the first line returns a number, so you're trying to call Array + Fixnum, which is not a defined operation.

  3. You're pushing number into your @fib array, but that variable isn't assigned a
    value anywhere.

If you want a method that generates an array of the first n Fibonacci numbers, here's one Ruby way to do it:

def fib(n)
  (n == 1) ? [0] : (2..(n-1)).each_with_object([0,1]) { |i,a| a[i] = a[i-2] + a[i-1] }
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.