On Mar 28, 10:02 pm, John Merlino <[email protected]> wrote:
> 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

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

>
> 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.
>
> --
> Posted viahttp://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to