AnApprentice AnApprentice - 1 year ago 97
Ruby Question

Checking if a variable is an integer

Does Rails 3 or Ruby have a built-in way to check if a variable is an integer?

For example,

1.is_an_int #=> true
"".is_an_int #=> false?

Answer Source

You can use the is_a? method

>> 1.is_a? Integer
=> true
>> "".is_a? Integer
=> false
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download