Submitted by legacy (Mon Nov 25 13:11:35 UTC 2002)
I'd like to be able to pick numbers out of strings easily. Presently, if I know the number will be an integer, I can use to_i. If I know it will be a float, I can use to_f. But if I don't know which has been supplied, and all I want is something numeric, any of
1, 1.023, 123456789123456789123456789123456789, 3e96
could be valid input, and I'd like to get the "correct" class: Integer, Fixnum, Bignum or Float.
The parser "knows" how to do this, so it must be achievable, but it would be good to avoid eval.
So I'd like to suggest String#to_n for this purpose. I think I can't use to_f and convert to integer afterwards, because big integers can be larger than a float will hold.
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .