From: Stefano Crocco Date: 2007-11-23T02:49:31+09:00 Subject: Re: Checking whether a string is a number in disguise? Alle giovedì 22 novembre 2007, Peter Bunyan ha scritto: > I'm working on an RPN calculator (don't ask why...) and I'm having > trouble getting a number from gets - how am I supposed to know whether > it's a number? > > At the moment I'm doing [eval(oper) == oper.to_f], which is far from > ideal. Any ideas? You all get to win my calculator if you know of a > better way :D. Do you mean you need to find out whether a string contains a number or not? In this case, you can either use Kernel#Float, which returns str.to_f if the string contains a number and raises an exception owtherwise, or try to craft a regexp which only matches strings containing numbers. For example, the (untested) regexp /[+-]?\d+(.\d+)?(e[+-]?\d+)?/ should match numbers with an optional + or - in front, followed by at least one digit, with an optional decimal part (if there's a dot, there should be at least one digit following it; if you want to allow something like 11. then replace (.\d+) with (.\d*) ). The number can also be in exponential form, with a downcase e and an optional + or - in front of the exponent. I hope this helps Stefano