PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Checking whether a string is a number in disguise?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Checking whether a string is a number in disguise?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 17h36   #1
Peter Bunyan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Checking whether a string is a number in disguise?

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 .
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 17h49   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

Alle gioved=C3=AC 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) =3D=3D oper.to_f], which is far from
> ideal. Any ideas? You all get to win my calculator if you know of a
> better way .


Do you mean you need to find out whether a string contains a number or not?=
In=20
this case, you can either use Kernel#Float, which returns str.to_f if the=20
string contains a number and raises an exception owtherwise, or try to craf=
t=20
a regexp which only matches strings containing numbers. For example, the=20
(untested) regexp

/[+-]?\d+(.\d+)?(e[+-]?\d+)?/

should match numbers with an optional + or - in front, followed by at least=
=20
one digit, with an optional decimal part (if there's a dot, there should be=
=20
at least one digit following it; if you want to allow something like 11. th=
en=20
replace (.\d+) with (.\d*) ). The number can also be in exponential form,=20
with a downcase e and an optional + or - in front of the exponent.

I hope this s

Stefano

  Réponse avec citation
Vieux 22/11/2007, 18h05   #3
Peter Bunyan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

Is that the only way to do it? There's no built-in method for telling if
a string is a number?

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 18h12   #4
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

Alle gioved=C3=AC 22 novembre 2007, Peter Bunyan ha scritto:
> Is that the only way to do it? There's no built-in method for telling if
> a string is a number?


As far as I know, no.

Stefano

  Réponse avec citation
Vieux 22/11/2007, 18h30   #5
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

2007/11/22, Peter Bunyan <peter.bunyan@gmail.com>:
> 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 .


str = gets # chomp is not needed for Float
num = Float(str) rescue nil

if num
puts "I'm a number: #{num}"
end

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 22/11/2007, 19h11   #6
Peter Bunyan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

OK, this seems to work:

class String
def is_number?
Float(self) == self.to_f
rescue false
end
end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 19h20   #7
Ryan Davis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?


On Nov 22, 2007, at 10:05 , Peter Bunyan wrote:

> Is that the only way to do it? There's no built-in method for
> telling if
> a string is a number?


Strings are never numbers in ruby. Ever. That is the beauty of working
in a clean typesafe language.

What you're doing, possibly without realizing it, is writing a parser
for RPN but it sounds like you've not addressed this project like a
parser. I suggest you go look at the screencast for treetop (a parser
generator). In this screencast they build up an infix parser, which is
much more than you need for RPN, but the tool is clean, powerful, and
worth learning.

http://www.pivotalblabs.com/files/tr...ic-example.mov


  Réponse avec citation
Vieux 22/11/2007, 22h12   #8
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

On 22.11.2007 20:11, Peter Bunyan wrote:
> OK, this seems to work:
>
> class String
> def is_number?
> Float(self) == self.to_f
> rescue false
> end
> end


That's an error prone approach because it relies on comparing floats
(which is also superfluous here because the real work is done by Float
and rescue). Why don't you just do

def float?
Float(self) rescue nil
end

Cheers

robert
  Réponse avec citation
Vieux 23/11/2007, 00h54   #9
Raul Parolari
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?


Stefano Crocco wrote:
> Alle giovedì 22 novembre 2007...
> In this case, you can either use Kernel#Float, which returns str.to_f
> .. or try to craft a regexp which only matches strings containing
> numbers.
> For example, the (untested) regexp
>
> /[+-]?\d+(.\d+)?(e[+-]?\d+)?/


Two small additions:
a) the 'dot' should be literally a dot (else it matches any character)
b) the boundaries to the whole expresssion are needed (else "ab23.35de"
would be
validated).

So, the regexp should be something like:

/^ [+-]? \d+ (?: [.]\d+)? (?: e[+-]? \d+)? $ /x

(If capture of components is desired, parenthesis should enclose all
components, and the ?: removed).

Raul
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 23/11/2007, 04h00   #10
Lloyd Linklater
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Checking whether a string is a number in disguise?

I do not think you can unless you can assume that it is base 10 or less.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14094 seconds with 18 queries