R
Ryo
Hello all,
I'm wondering if there's a strict version of to_f,
such as
"1.3".strict_to_f # -> 1.3
"hello".strict_to_f # raises an exception
One could implement it as
def String.strict_to_f
if self =~ regex_floating_point
return self.to_f
else
raise some_exception
end
end
But, 1) it's not trivial (to me) to construct a correct
regex_floating_point and 2) somebody may already have done
this (that is, I don't want to reinvent the wheel).
I'm my applications, raising an exception to
"hello".to_f
is *always* better than returning 0.0 .
Cheers,
Ryo
I'm wondering if there's a strict version of to_f,
such as
"1.3".strict_to_f # -> 1.3
"hello".strict_to_f # raises an exception
One could implement it as
def String.strict_to_f
if self =~ regex_floating_point
return self.to_f
else
raise some_exception
end
end
But, 1) it's not trivial (to me) to construct a correct
regex_floating_point and 2) somebody may already have done
this (that is, I don't want to reinvent the wheel).
I'm my applications, raising an exception to
"hello".to_f
is *always* better than returning 0.0 .
Cheers,
Ryo