ruby check if string is an integer code example
Example 1: ruby is int
1.is_a? Integer
=> true
"[email protected]".is_a? Integer
=> false
nil.is_a? Integer
=> false
Example 2: ruby test is number
12.is_a? Numeric
Example 3: ruby check if string is integer
class String
def numeric?
Float(self) != nil rescue false
end
end
"Hello World!".numeric?