You can use is_a? or there is kind_of? and instance_of? methods
so you could test as follows
arr.each do |a|
if a.is_a? Integer
#do stuff here
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/.
>