This may seem overly critical, but based on your description, you will almost certainly do something βwrongβ ... Why do you need to check if String is a valid representation of Float , but String is not actually converted?
This is just a hunch, but it may be helpful to describe what you are trying to accomplish. This is probably the "best" way.
For example, you might need to insert a String into a database and compile a sql statement using string concatenation. It would be preferable to convert to Float and use prepared statements.
If you just need to check the "shape" of the string, it would be preferable to use a regular expression, because, as Dylan pointed out, Float can be represented in any number of ways, which may or may not be necessary.
You will also have to deal with international input, in Europe a comma is used instead of a period as a decimal separator, but Ruby Floats should use (.).
a2800276
source share