Ruby on Rails Thursday, March 31, 2011

On 30 March 2011 16:45, John Merlino <lists@ruby-forum.com> 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

--
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