How to check an input IS an integer?

Hi everyone !
I have a little problem since a few days. I would like to check that my
user (I am writing a small program) had type an integer while I asked
him.
So, I thought I could check the class or the ‘type_of’.
But these methods don’t work.

Here is my try :
cat check_class.rb

#!/usr/bin/env ruby

-- coding: utf-8 --

check_class.rb

Tests sur les class

Ici, on verifie que le flux envoyé

par le clavier est bien un entier.

Autrement, on boucle.

print "Veuillez entrer un entier (1) : "
reponse = readline.chomp

while reponse.kind_of?(Integer) == false do
print "Veuillez entrer un entier (2) : "
reponse = readline.chomp
end

If someone has an idea… :confused:


Sources :
http://fr.wikibooks.org/wiki/Mathématiques_avec_Python_et_Ruby/Nombres_entiers_en_Ruby
(‘tests sur les entiers’ section)

http://blog.escarworld.com/post/2011/04/19/is_a,-kind_of,-connaitre-la-classe-d-une-variable


Cordialement.
±------------------------------------------------+
| Ensemble, libérons Internet ! |
±---------------±-------------------------------+
| Linux à Nantes | http://www.linux-nantes.org/ |
±---------------±-------------------------------+
| Projet Bépo | http://bepo.fr/ |
±---------------±-------------------------------+
| Site perso | http://captain.ishido.free.fr |
±---------------±-------------------------------+
| Message tapé en Bépo sur un Typematrix 2030 USB |
±------------------------------------------------+

Sent from my iPad

On 30/07/2013, at 10:03 PM, Captain I. [email protected] wrote:

print "Veuillez entrer un entier (1) : "
reponse = readline.chomp

Try…

puts response.class

That might tell you what the problem is.

Henry

Am 30.07.2013 12:03, schrieb Captain I.:

Hi everyone !
I have a little problem since a few days. I would like to check that my
user (I am writing a small program) had type an integer while I asked him.

There are several possibilities, the one I like best in this case
is using a Regex (Note that user input is always a String):

2.0.0p247 :005 > answer = ‘123’
=> “123”
2.0.0p247 :006 > /\A[0-9]+\z/ === answer
=> true
2.0.0p247 :007 > answer = ‘123abc’
=> “123abc”
2.0.0p247 :008 > /\A[0-9]+\z/ === answer
=> false

Other possibilities can easily be found by searching the archives,
e.g. using Integer(), (which involves rescuing exceptions, which I
do not like for this use case too much).

Regards,
Marcus

Le 30/07/2013 12:19, Henry M. a

Right! Regular expressions may help you here. Also/alternatively have a
look at
Integer()http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-Integerand
String#to_i
http://www.ruby-doc.org/core-2.0/String.html#method-i-to_i.

Bernard

Am 30.07.2013 20:51, schrieb Captain I.:

=> “123”

answer=answer.to_i
while answer == 0 do
puts "Please type an integer : "
answer = readline.chomp
answer=answer.to_i
end

Depending on the use case, `0’ might be a valid answer…

Something like this should work:

answer = nil
until /\A[0-9]+\z/ === answer
print 'Enter an integer: ’
answer = gets.chomp
end

number = answer.to_i
puts “Your answer: #{number}”

Regards,
Marcus

Le 30/07/2013 12:55, [email protected] a écrit :

2.0.0p247 :006 > /\A[0-9]+\z/ === answer
Regards,
Marcus

I’ve found a ‘solution’. I don’t like it but cough it works.
If you’re a REAL programmer, don’t read.

Let’s do DIRTY things

(Don’t try do to this at home, kids !)

answer=""
answer=answer.to_i
while answer == 0 do
puts "Please type an integer : "
answer = readline.chomp
answer=answer.to_i
end


Cordialement.
±------------------------------------------------+
| Ensemble, libérons Internet ! |
±---------------±-------------------------------+
| Linux à Nantes | http://www.linux-nantes.org/ |
±---------------±-------------------------------+
| Projet Bépo | http://bepo.fr/ |
±---------------±-------------------------------+
| Site perso | http://captain.ishido.free.fr |
±---------------±-------------------------------+
| Message tapé en Bépo sur un Typematrix 2030 USB |
±------------------------------------------------+

Other possibilities can easily be found by searching the archives,
e.g. using Integer(), (which involves rescuing exceptions, which I
do not like for this use case too much).

Regards,
Marcus

Isn’t that the whole point of Integer() ?
A user entering invalid input is an Exception. If the programmer wants
to handle it and retry then that is a valid use of Exception handling.

I also prefer the shorter Regexp notation: \d rather than [0-9]

Am 30.07.2013 22:44, schrieb Joel P.:

Other possibilities can easily be found by searching the archives,
e.g. using Integer(), (which involves rescuing exceptions, which I
do not like for this use case too much).

Regards,
Marcus

Isn’t that the whole point of Integer() ?
A user entering invalid input is an Exception. If the programmer wants
to handle it and retry then that is a valid use of Exception handling.

I think opinions diverge on this. Some people would argue that you
must expect a user to enter invalid input, ergo: no exception.
A matter of taste.

However: when you decide on Integer(), make sure to rescue the
correct exception class, rescuing all exceptions can be problematic
(it could e.g. mute exceptions caused by a typo in the code).

Regards,
Marcus

Le 30/07/2013 22:35, [email protected] a écrit :

answer = readline.chomp
print 'Enter an integer: ’
answer = gets.chomp
end

number = answer.to_i
puts “Your answer: #{number}”

Regards,
Marcus

Well, I wasn’t sure for your ‘/\A[0-9]+\z/’ expression, so I checked it
out on http://www.ruby-doc.org/core-1.9.3/Regexp.html.
Now, I think I understand. Thanks a lot :slight_smile: ! I can continue my little
program.


Cordialement.
±------------------------------------------------+
| Ensemble, libérons Internet ! |
±---------------±-------------------------------+
| Linux à Nantes | http://www.linux-nantes.org/ |
±---------------±-------------------------------+
| Projet Bépo | http://bepo.fr/ |
±---------------±-------------------------------+
| Site perso | http://captain.ishido.free.fr |
±---------------±-------------------------------+
| Message tapé en Bépo sur un Typematrix 2030 USB |
±------------------------------------------------+