Sebastian Hungerecker
1/11/2009 10:34:00 PM
Ulrich Holtzhausen wrote:
> puts "What is your favourite number?"
> num =3D gets.to_i
> if num !=3D Integer
> =C2=A0 puts "Please type a number"
> else
> =C2=A0 better =3D num + 1
> =C2=A0 puts "Hah! #{num} is pathetic! #{better} is a much better number!!=
!"
> end
>
> Somewhere along the line there's a mistake however, but I take it you
> guru's can see what I'm trying to do here?
>
> How can I check if the variable 'num' is an integer and if not, ask the
> user to re-input with an integer?
to_i always returns an Integer (if the string isn't a valid number, it just=
=20
returns 0). If you don't want that you can use Integer(str) which raises an=
=20
exeption if the string is not a valid number.
Also note that 4 =3D=3D Integer is false. Only Integer is =3D=3D Integer.
num.is_a? Integer would work, but as I said, to_i always returns an Integer,
so that would always be true.
HTH,
Sebastian
=2D-=20
NP: Die Apokalyptischen Reiter - Auferstehen Soll In Herrlichkeit
Jabber: sepp2k@jabber.org
ICQ: 205544826