# Can we define data types explicitly?

**URL:** <https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828>\
**Category:** Ruby\
**Created:** [October 31, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828 "2007-10-31T13:55:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 31, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/1 "2007-10-31T13:55:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 31, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/2 "2007-10-31T14:07:10Z")

</div>

2007/10/31, Shuaib Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> after i split them in an array  
> Is there any mechanism that allows me to check the data type ?  
> 13:57:22 /cygdrive/c/SCMws/RKlemme$ ruby -e ‘Integer(“rubbish”)’  
> -e:1:in `Integer’: invalid value for Integer: “rubbish” (ArgumentError)  
> from -e:1  
> 14:06:07 /cygdrive/c/SCMws/RKlemme$

Kind regards

robet

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 31, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/3 "2007-10-31T14:55:51Z")

</div>

Shuaib Z. wrote:

> f = gets  
> “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.

There is no conversion going on there. gets returns a string and that  
string  
is then split into an array of substrings. There was never any object  
involved that wasn’t a string.

> I need to check whether the type of 5 is integer or not, 8.9 is float,  
> etc.

The class of 5 is Integer. The class of “5” is String. If you want to  
check  
whether a given string is a valid string-representation of an Integer  
you  
can use Interger(str), which will raise if str is not a valid integer-  
representation.

> Is there any mechanism that allows me to check the data type ?

Object#class and Object#is\_a? but both of those will tell you that your  
object is a string because it is. There is no method which tells you  
which  
class your string is valid string-represantation of since that can’t be  
unambiguously determined. You can only check whether your string is a  
valid  
represantation of an object of ClassA and if it isn’t check if it is for  
ClassB and so on.  
So in your case you could do something like:  
arr = gets.split(/\s\*,\s\*/).map do |str|  
case str  
when /^"([^"]\*)"$/  
$1  
when /./  
Float(str)  
else  
Integer(str)  
end  
end

This assumes that everything that is in quotes is a string and  
everything else  
is a number - a float if there’s dot in it, an integer otherwise. This  
will  
blow up, though, if a string contains a comma (so will yours btw). It  
will  
also accept “foo"bar” as a valid string. If you change the re to be  
non-greedy  
it won’t accept that anymore, but it also won’t accept “foo"bar”. It  
also  
won’t realize that 5e4 is supposed to be a float as it doesn’t contain a  
dot.  
If you’d change the regex to look for 'e’s as well as dots, it will  
mistake  
hex-numbers containing 'e’s as floats.

Another solution would be  
arr = gets.split(/\s\*,\s\*/).map {|str| eval str}  
This will also blow up on commas inside strings, but other than that it  
will  
handle any valid ruby expression correctly. However, this is extremely  
unsafe  
for obvious reasons, so you shouldn’t do it.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 1, 2007, 2:26am UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/4 "2007-11-01T02:26:39Z")

</div>

Thanks guys for the replies. I believe Integer and Float way will work  
better than to\_i or to\_f. Because Integer and Float will check the  
parameter before conversion.

Regards

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 1, 2007, 10:38am UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/5 "2007-11-01T10:38:41Z")

</div>

Hi

Just to share with you guys.

After trying several ways of checking the data types of my input. I  
found that the best method to be used is is\_a? or kind\_of? because it  
returns true or false value. In my case, this is best.

If you are using Float(“ss”) or Integer(“5”). It will work but unluckly  
it might crash your program which happen with me. and to avoid that you  
need to use exception which will enlarge your code.

so just to list the ways down  
to check datatypes you can use

x = “name”  
y = 5

1. 

x.class =\> String  
y.class =\> Float

1. 

Integer(y) =\> 5  
Float(y) =\> 5.0

1. 

y.is\_a? Integer =\> true  
x.is\_a? String =\> true  
y.is\_a? Float =\> flase

Cheers

This forum is a continuous learning platform and is very helpful.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 1, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/6 "2007-11-01T19:48:09Z")

</div>

Shuaib Z. wrote:

> After trying several ways of checking the data types of my input. I  
> found that the best method to be used is is\_a? or kind\_of? because it  
> returns true or false value. In my case, this is best.

User input will always be a string until you convert it.  
user\_input = gets  
user\_input.is\_a? Integer  
Will never be true - even if the user enters a number.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 1, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/7 "2007-11-01T20:27:30Z")

</div>

On Nov 1, 2007, at 1:47 PM, Sebastian H. wrote:

> Shuaib Z. wrote:
> 
> > After trying several ways of checking the data types of my input. I  
> > found that the best method to be used is is\_a? or kind\_of? because it  
> > returns true or false value. In my case, this is best.
> 
> User input will always be a string until you convert it.  
> user\_input = gets  
> user\_input.is\_a? Integer  
> Will never be true - even if the user enters a number.

Oh really?! Perhaps if it comes from a keyboard and stdin, yes.  
What about other input devices ??  
Mouse, Joystick/joypad, Stylus, Light pen, other types of sensors  
(heat, pressure, wind speed…)  
Changing input routines? There is no reason input could not be any  
other class in Ruby, including nil.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/can-we-define-data-types-explicitly/118828/8 "2007-11-01T20:55:39Z")

</div>

On Nov 1, 1:12 pm, removed\_email\_address@domain.invalid wrote:

> Changing input routines? There is no reason input could not be any  
> other class in Ruby, including nil.

Of course. Obviously (?) Ruby can communicate with the outside world  
in programmatic ways that don’t necessarily involve strings. Network  
packets are not strings that need to be parsed. Reading a binary file  
does not go through an intermediary string representation.

However, the ‘s’ in ‘gets’ stands for ‘string’. I believe Sebastien  
was commenting on the original poster’s original apparent impression  
that if the user types “5.5” from the keyboard, it would be a Float.
