# Can we define data types explicitly?

**URL:** <https://rubytalk.org/t/can-we-define-data-types-explicitly/41814>\
**Category:** ruby-talk\
**Created:** [31 October 2007 13:04 UTC](https://rubytalk.org/t/can-we-define-data-types-explicitly/41814 "2007-10-31T13:04:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cliveharber](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Cliveharber](https://rubytalk.org/u/Cliveharber)\
**Post date:** [31 October 2007 13:04 UTC](https://rubytalk.org/t/can-we-define-data-types-explicitly/41814/1 "2007-10-31T13:04:54Z")

</div>

You can use is\_a? or there is kind\_of? and instance\_of? methods

so you could test as follows

arr.each do |a|  
&nbsp;&nbsp;&nbsp;&nbsp;if a.is\_a? Integer  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do stuff here  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

You can also use this in a case construct.

the 'Integer' is the type - you can also have things like Bignum, Fixnum, Numeric, String, Object, Array etc.

HTH

Clive

---- Shuaib Zahda \<shuaib.zahda@gmail.com\> wrote:

> **···**
>
> > Hi
> > 
> > I am working on application that emphasizes on the data types of the  
> > data. The application reads a line from the user by the prompt. then it  
> > divides the entry into parts based on comma separator.  
> > e.g
> > 
> > f = gets  
> > \<user input\> "apple", 5, 8.9, "red"
> > 
> > after i split them in an array
> > 
> > arr = f.split(", ")
> > 
> > they will be all converted into string and stored in arr as elements of  
> > array.  
> > I need to check whether the type of 5 is integer or not, 8.9 is float,  
> > etc. In this case it is fine but in the case that the user keys in  
> > string instead of float. the methods to\_i, to\_f will convert the string  
> > into 0 and assume it is of that type.
> > 
> > Is there any mechanism that allows me to check the data type ?
> > 
> > Regards  
> > Shuaib  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
