Why does my method return an error if argument is not a number?

Hey all,

I get the following error message:
ActionView::TemplateError (class or module required for rescue clause)
in app/vie$ app/helpers/format_helper.rb:116:in `divide_numbers’

Basically I have a field in database called Student Fails and I populate
fields with data. Sometimes the value can be empty - not null, not
integer, just empty because when user updates record they clear out
field and update it. First in my progressbar file I render a partial
called progressbar_item, passing in the string to the partial in render
method as well as data parameters that I can use in my progressbar file:

  = render "dashboard/progressbar_item.html", :actual =>

@student_counters[:student_failed], :expected =>
cu.context.expected_student_failed, :title => “Student Fails”

Now with the data parameters available in my view, I call the
divide_numbers method with the values of those parameters so if
expected_student_failed was an empty value in database, that empty value
gets passed as the second argument:

= render “dashboard/progressbar.html”, :value =>
divide_numbers(actual.to_f, expected), :text => “#{actual} /
#{expected}”

Then the method is executed in format_helper:

def divide_numbers(x, y)
result = x / y
result.to_s == ‘NaN’ ? 0 : result
rescue 0
end

If y is nothing (empty), the error occurs. If y is an integer the error
doesn’t occur. Doesn’t this line take all non numbers like empty and
convert it to integer 0:
result.to_s == ‘NaN’ ? 0 : result

If that’s the case, then why do I get the error?

Will I be forced to do this (i haven’t tested this yet but I presume it
would work):

def divide_numbers(x, y)
result = x / y
if result.nil?
result.to_s = 0
else
result.to_s == ‘NaN’ ? 0 : result
end
rescue 0
end

Thanks for response.

On Mon, Mar 28, 2011 at 4:02 PM, John M. [email protected]
wrote:

field and update it. First in my progressbar file I render a partial
expected_student_failed was an empty value in database, that empty value

This line will raise an error if y is nil or blank… It won’t even get
to
the next line.

On Mar 28, 10:02pm, John M. [email protected] wrote:

called progressbar_item, passing in the string to the partial in render

end
That’s not legal ruby - where you’ve stuck a 0 it’s expecting a class
name (which sort of exceptions the rescue clause should handle).
You may be mixing things up with the one liner do_foo rescue 0. Also
floats have a nan? method which would be a little nicer than that
string comparison you have there

If y is nothing (empty), the error occurs. If y is an integer the error
doesn’t occur. Doesn’t this line take all non numbers like empty and
convert it to integer 0:
result.to_s == ‘NaN’ ? 0 : result

Not quite. That will only coerce NaN to 0, but it wouldn’t do anything
to Infinity, and of course you wouldn’t even get to this line if y was
something such that x / y was meaningless (ie y is not coercible to a
numeric value). NaN isn’t a placeholder for anything that isn’t a
number but for specific situations where it isn’t possible to come up
with a more meaningful answer (eg 0.0/0 )

Fred

On 28 March 2011 22:02, John M. [email protected] wrote:

If y is nothing (empty), the error occurs. If y is an integer the error
doesn’t occur.

yup. “divide by zero” will do that…

Doesn’t this line take all non numbers like empty and
convert it to integer 0:
result.to_s == ‘NaN’ ? 0 : result
If that’s the case, then why do I get the error?

Unfortunately, your code is breaking before there, and I doubt it
would work as you expect even if it did (I don’t think you can be sure
the integer would be ‘NaN’)

Try this instead:

def divide_numbers(x, y)
(x / y) rescue 0
end

Thanks for all the responses. My understanding it’s breaking when I try
to pass nothing as an argument to that function because you can’t pass
an undefined variable as an argument.

On 30 March 2011 16:45, John M. [email protected] wrote:

Thanks for all the responses. My understanding it’s breaking when I try
to pass nothing as an argument to that function because you can’t pass
an undefined variable as an argument.

What’s the error message you get? “divide by zero”? or “1 parameter for
2”?

You earlier said:

Sometimes the value can be empty - not null, not
integer, just empty because when user updates record they clear out
field and update it.

= render “dashboard/progressbar.html”, :value =>
divide_numbers(actual.to_f, expected), :

In this event, you are passing two parameters to the method, one as
a float (which, if it started as an empty string, will be turned to
0.0) and the expected value, which may be nil (or an empty string), or
a number. Is there anywhere you are ensuring it’s a float like you do
the “actual” value?
Maybe try:
divide_numbers(actual.to_f, expected.to_f)
…to get more consistent results from a range of input values.

BTW, have a play in the console too, to see what different
combinations give you:

2/0
ZeroDivisionError: divided by 0
2/“”
TypeError: String can’t be coerced into Fixnum
2/nil
TypeError: nil can’t be coerced into Fixnum
2/0.0
=> Infinity

…try that with your divide_numbers method…

HTH