Editing Topic: RCR123 Project: RCR | RCRchive home

RCR 123: Suggest String#to_n to encompass to_i and to_f

submitted by HughSasse on 2002-11-25 08:19:35


This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.


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.



Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog