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.
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.
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:
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):
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
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]
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 |
+-------------------------------------------------+
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):
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
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.
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 ! 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}"