Can we define data types explicitly?

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

2007/10/31, Shuaib Z. [email protected]:

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

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

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

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

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

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

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.

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.

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.

On Nov 1, 1:12 pm, [email protected] 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.