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)

--
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 |

+-------------------------------------------------+

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

Try...

puts response.class

That might tell you what the problem is.

Henry

···

Sent from my iPad
On 30/07/2013, at 10:03 PM, Captain Ishido <ishido7@yahoo.fr> wrote:

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

···

Am 30.07.2013 12:03, schrieb Captain Ishido:

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.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Huuuuuuuuuum, I see. /readline/ says it's a string, whatever I'm typing.
So, I think I have to analyse what my user types, and IF it's number, I
cast it to Integer.
Right ?

···

Le 30/07/2013 12:19, Henry Maddocks a écrit :

Sent from my iPad

On 30/07/2013, at 10:03 PM, Captain Ishido <ishido7@yahoo.fr> wrote:

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

Try...

puts response.class

That might tell you what the problem is.

Henry

--
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 |

+-------------------------------------------------+

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

···

Le 30/07/2013 12:55, sto.mar@web.de a écrit :

Am 30.07.2013 12:03, schrieb Captain Ishido:

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

-----------------------------------------------
# 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]

···

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

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-Integer&gt;and
String#to_i <http://www.ruby-doc.org/core-2.0/String.html#method-i-to_i&gt;\.

Bernard

···

On Tue, Jul 30, 2013 at 1:05 PM, Captain Ishido <ishido7@yahoo.fr> wrote:

Le 30/07/2013 12:19, Henry Maddocks a écrit :
>
>
> Sent from my iPad
>
> On 30/07/2013, at 10:03 PM, Captain Ishido <ishido7@yahoo.fr> wrote:
>
>>
>> print "Veuillez entrer un entier (1) : "
>> reponse = readline.chomp
>>
>
> Try...
>
> puts response.class
>
> That might tell you what the problem is.
>
> Henry

Huuuuuuuuuum, I see. /readline/ says it's a string, whatever I'm typing.
So, I think I have to analyse what my user types, and IF it's number, I
cast it to Integer.
Right ?

--
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 |
+-------------------------------------------------+

--
Computer Science Department, EPL/INGI, UCLouvain, Belgium
Mail: blambeau@gmail.com
Mobile: +32 477 24 58 61
Blog: http://revision-zero.org/
Code: blambeau (Bernard Lambeau) · GitHub
Follow: http://twitter.com/blambeau/

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

···

Am 30.07.2013 20:51, schrieb Captain Ishido:

Le 30/07/2013 12:55, sto.mar@web.de a écrit :

Am 30.07.2013 12:03, schrieb Captain Ishido:

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

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

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

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

···

Am 30.07.2013 22:44, schrieb Joel Pearson:

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.

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

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.

···

Le 30/07/2013 22:35, sto.mar@web.de a écrit :

Am 30.07.2013 20:51, schrieb Captain Ishido:

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

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

--
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 |

+-------------------------------------------------+