Shuaib Zahda wrote:
> 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.

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
-- 
Jabber: sepp2k / jabber.org
ICQ: 205544826