David Vallner
2/10/2006 1:21:00 PM
Dna Piatok 10 Február 2006 04:08 ara.t.howard@noaa.gov napísal:
> hmmm. if that's the case (possible string) i'd use
>
> target_f += Float(source_f)
>
> since
>
> harp:~ > ruby -r yaml -e' target_f = 40.0; y target_f += "junk".to_f
> ' --- 40.0
>
> and
>
> harp:~ > ruby -r yaml -e' target_f = 40.0; y target_f += Float("junk")
> ' -e:1:in `Float': invalid value for Float(): "junk" (ArgumentError) from
> -e:1
>
> but
>
> harp:~ > ruby -r yaml -e' target_f = 40.0; y target_f += Float("2") '
> --- 42.0
>
> cheers.
>
> -a
Well, to_f coerces any junk to 0.0. It depends in that case whether you want
magic or exact behavior. However, Float(nil) results in a TypeError.
But this should work as desired -and- not accept any line noise for source_f:
target_f += Float(source_f || nil)
David Vallner