Checking if input == Integer/String?

U

Ulrich Holtzhausen

Hi,

I'm all new to programming and all new to Ruby - hope I haven't made a
mistake by choosing this language.

I'm trying to teach myself by creating tiny (mostly useless) little
applications and then extend on them as I learn.

Today I started out and this is what I got:

#!/usr/bin/env ruby

#Favourite number
puts "What is your favourite number?"
num = gets.to_i
if num != Integer
puts "Please type a number"
else
better = num + 1
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?

Thanks in advance!
 
S

Sebastian Hungerecker

Ulrich said:
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: (e-mail address removed)
ICQ: 205544826
 
T

Tim Hunter

Ulrich said:
Hi,

I'm all new to programming and all new to Ruby - hope I haven't made a
mistake by choosing this language.

I'm trying to teach myself by creating tiny (mostly useless) little
applications and then extend on them as I learn.

Today I started out and this is what I got:

#!/usr/bin/env ruby

#Favourite number
puts "What is your favourite number?"
num = gets.to_i
if num != Integer
puts "Please type a number"
else
better = num + 1
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?

Thanks in advance!

The mistake is that Integer is a class in Ruby, so your test "num !=
Integer" is testing to see if num is not equal to the Integer class. It
never will be equal, of course.

The gets method always returns a string. You are calling .to_i on the
string to convert it to an integer, which will work the way you expect
as long as the string is sequence of digits (with an optional leading
sign, of course.) However, .to_i happily "converts" non-integer strings
as well. Bring up irb and try it yourself:

irb(main):001:0> "20lbs. of fertilizer".to_i
=> 20
irb(main):002:0> "x".to_i
=> 0

Oops! It ignored the non-digit characters after "20" and converted "x"
to 0. Not what you wanted to happen, but it's working as documented:

~$ ri String.to_i
------------------------------------------------------------ String#to_i
str.to_i(base=10) => integer
------------------------------------------------------------------------
Returns the result of interpreting leading characters in str as an
integer base base (2, 8, 10, or 16). Extraneous characters past
the end of a valid number are ignored. If there is not a valid
number at the start of str, 0 is returned. This method never
raises an exception.

What you want is the Integer() method, which will raise an exception
when given an invalid argument. Then you catch the exception.

begin
n = Integer(str)
rescue ArgumentError
puts "Not an integer!"
end

Good luck!
 
U

Ulrich Holtzhausen

Thanks to both of you, I will try Integer(str) (just a bit unsure of how
to implement it but I'll give it a go and return here if I wasn't
successful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top