`+': Range can't be coerced into Fixnum (TypeError)

H

Harry Kakueki

I am getting the follower
`+': Range can't be coerced into Fixnum (TypeError)

How to get rid of this?

What is your method returning?

def checkforcube(rem)
if (rem<0)
return 0
end
for i in 1..rem
if (rem==(i**3))
return 1
end
end
end

## For example

check = checkforcube(7)
p check.class
p check



Harry
 
B

Brian Candler

Prateek said:
I am getting the follower
`+': Range can't be coerced into Fixnum (TypeError)

How to get rid of this?

You need to take a methodical approach to debugging. Fortunately, your
code is small and complete so I can run it here to get the backtrace.

$ ruby Question5.rb
Question5.rb:19:in `+': Range can't be coerced into Fixnum (TypeError)
from Question5.rb:19
from Question5.rb:16:in `each'
from Question5.rb:16
from Question5.rb:14:in `loop'
from Question5.rb:14

So what's line 19?
flag=flag+check

Clearly, one of these values is bad. So now insert a statement before
this line to show you the values just before the error occurs, like
this:

STDERR.puts "flag=#{flag.inspect}, check=#{check.inspect}"
flag=flag+check

$ ruby Question5.rb
flag=0, check=1
flag=0, check=1..2
Question5.rb:20:in `+': Range can't be coerced into Fixnum (TypeError)
from Question5.rb:20
from Question5.rb:16:in `each'
from Question5.rb:16
from Question5.rb:14:in `loop'
from Question5.rb:14

There's your problem. The second time round the loop, check is the range
1..2 and you are trying to calculate 0 + (1..2) which doesn't make
sense.

So now you ask, why does check have this value? That's easy, it's
assigned in the line before, and it's the return value from
checkforcube(remainder). So this function is returning a range, which
you weren't expecting.

Now you just have to remember that the return value of a method is the
value of the last expression evaluated; and the value of a 'for' loop is
the object being iterated over.
hi
hi
hi
=> 1..3
^^^^
hi
hi
hi
=> 0
^

You can probably do this sort of debugging with -rdebug or an IDE or
some other breakpoint facility, but I just use puts. There's less
learning, and it works in just about every environment.

HTH,

Brian.
 
B

Brian Candler

P.S. It's not normal ruby practice to return 0 or 1 for boolean
conditions. You should return false (or nil), and true (or any value
other than nil or false)

Then you can say:

if check

rather than

if check == 1

Regards,

Brian.
 
K

Ken Bloom

I am getting the follower
`+': Range can't be coerced into Fixnum (TypeError)

How to get rid of this?

Attachments:
http://www.ruby-forum.com/attachment/3915/Question5.rb

def checkforcube(rem)
if (rem<0)
return 0
end
for i in 1..rem
if (rem==(i**3))
return 1
end
end
puts "shouldn't get here"
end

#rest of the program as normal

output:

shouldn't get here
/tmp/v84078/ruby1.rb:20:in `+': nil can't be coerced into Fixnum (TypeError)
from /tmp/v84078/ruby1.rb:20
from /tmp/v84078/ruby1.rb:17:in `each'
from /tmp/v84078/ruby1.rb:17
from /tmp/v84078/ruby1.rb:15:in `loop'
from /tmp/v84078/ruby1.rb:15
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top