I have tried to create a method in Ruby which returns true if number is included fibonacci-sequence or false if it's not.
I think I got a problem when I return true or false.
Could anybody tell me why the first code doesn't work please?
def is_fibonacci?(num, array=[0,1])
n = array.length - 1
if array[n] > num
array.include?(num) ? true : false
end
array << array[n] + array[n-1]
is_fibonacci?(num, array)
end
When I run this code, I got this error message.
=>filename.rb:36:in `include?': Interrupt
def is_fibonacci?(num, array=[0,1])
n = array.length - 1
if array[n] > num
if array.include?(num)
return true
else
return false
end
end
array << array[n] + array[n-1]
is_fibonacci?(num, array)
end
Second code worked.
Why can't I use
array.include?(num) ? true : false
in the code?
Thank you for helping.