Ruby on Rails Monday, March 28, 2011

On 28 March 2011 22:02, John Merlino <lists@ruby-forum.com> 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

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment