# Checking if input == Integer/String?

**URL:** <https://rubytalk.org/t/checking-if-input-integer-string/51103>\
**Category:** ruby-talk\
**Created:** [11 January 2009 22:15 UTC](https://rubytalk.org/t/checking-if-input-integer-string/51103 "2009-01-11T22:15:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich\_Holtzhausen](https://avatars.discourse-cdn.com/v4/letter/u/85e7bf/32.png) [@Ulrich\_Holtzhausen](https://rubytalk.org/u/Ulrich_Holtzhausen)\
**Post date:** [11 January 2009 22:15 UTC](https://rubytalk.org/t/checking-if-input-integer-string/51103/1 "2009-01-11T22:15:17Z")

</div>

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  
&nbsp;&nbsp;puts "Please type a number"  
else  
&nbsp;&nbsp;better = num + 1  
&nbsp;&nbsp;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!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [11 January 2009 22:34 UTC](https://rubytalk.org/t/checking-if-input-integer-string/51103/2 "2009-01-11T22:34:12Z")

</div>

Ulrich Holtzhausen wrote:

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

to\_i always returns an Integer (if the string isn't a valid number, it just  
returns 0). If you don't want that you can use Integer(str) which raises an  
exeption if the string is not a valid number.  
Also note that 4 == Integer is false. Only Integer is == 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

> **···**
>
> --  
> NP: Die Apokalyptischen Reiter - Auferstehen Soll In Herrlichkeit  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [11 January 2009 22:34 UTC](https://rubytalk.org/t/checking-if-input-integer-string/51103/3 "2009-01-11T22:34:14Z")

</div>

Ulrich Holtzhausen wrote:

> 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  
> &nbsp;&nbsp;puts "Please type a number"  
> else  
> &nbsp;&nbsp;better = num + 1  
> &nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.to\_i(base=10) =\> integer

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the result of interpreting leading characters in str as an  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;integer base base (2, 8, 10, or 16). Extraneous characters past  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the end of a valid number are ignored. If there is not a valid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number at the start of str, 0 is returned. This method never  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
> &nbsp;&nbsp;&nbsp;&nbsp;n = Integer(str)  
> rescue ArgumentError  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Not an integer!"  
> end
> 
> Good luck!
> 
> --  
> RMagick: [http://rmagick.rubyforge.org/](http://rmagick.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Ulrich\_Holtzhausen](https://avatars.discourse-cdn.com/v4/letter/u/85e7bf/32.png) [@Ulrich\_Holtzhausen](https://rubytalk.org/u/Ulrich_Holtzhausen)\
**Post date:** [11 January 2009 22:53 UTC](https://rubytalk.org/t/checking-if-input-integer-string/51103/4 "2009-01-11T22:53:05Z")

</div>

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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
