I am getting the follower
`+’: Range can’t be coerced into Fixnum (TypeError)
How to get rid of this?
I am getting the follower
`+’: Range can’t be coerced into Fixnum (TypeError)
How to get rid of this?
On Fri, Jul 31, 2009 at 1:07 PM, Prateek A.[email protected]
wrote:
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
check = checkforcube(7)
p check.class
p check
Harry
Prateek A. wrote:
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.
def foo; (1…3).each { puts “hi” }; end
=> nilfoo
hi
hi
hi
=> 1…3
^^^^
def foo; (1…3).each { puts “hi” }; return 0; end
=> nilfoo
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.
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.
On Fri, 31 Jul 2009 13:07:33 +0900, Prateek A. wrote:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs