You can use
12.is_a? Numeric
(Numeric
will work for integers and floats.)
If it arrives as a string that might contain a representation of a valid number, you could use
class String
def valid_float?
true if Float self rescue false
end
end
and then '12'.valid_float?
will return true if you can convert the string to a valid float (e.g. with to_f
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…