ruby on rails how to deal with NaN

I have read few posts regarding NaN but did not figure out how to deal with it in Ruby on Rails. I want to check a value if it is a NaN I want to replace it with Zero(0). I tried the following

logger.info(".is_a? Fixnum #{percent.is_a? Fixnum}")

when percent has NaN it returns me false.

I have made few changes in the logger

logger.info("Fixnum #{percent.is_a? Fixnum} percent #{percent}")

Output

Fixnum false percent 94.44444444444444
Fixnum false percent NaN
Fixnum false percent 87.0

I found my solution.
NaN is instance of Float . Use Float#nan? method.

>> nan = 0.0/0 # OR nan = Float::NAN
=> NaN
>> nan.class
=> Float
>> nan.nan?
=> true
>> nan.is_a?(Float) && nan.nan?
=> true
>> (nan.is_a?(Float) && nan.nan?) ? 0 : nan
=> 0

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs