ruby picture

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

Submitted by legacy (Mon Nov 25 13:11:35 UTC 2002)

Abstract

This is a legacy RCR from Ruby Garden, submitted by HughSasse. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

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.

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .