9 ms·
Integer() or Float() let you do cool things like this: def is_int?(x) Integer(x); true; rescue; false end Which will let you know if some input
by Aaronneyer 13y ago
Integer() or Float() let you do cool things like this:
def is_int?(x)
Integer(x); true; rescue; false
end
Which will let you know if some input passed can be safely converted to an integer.
- pmh 13y agoA couple of minor nits: - is_int? is rather misleading (I would not expect is_int?(1.2) to be true) - Since Ruby ints are truthy, the idiomatic form would probably just return Integer(x) and false or nil otherwise: def is_int?(x) Integer(x) rescue false end
- maurycy 13y agos/is_int/intable/ ;-)
- Aaronneyer 13y agoAh you're right. Hadn't actually tested this. The way that I've actually used it is for `is_num?` and using `Float()`. I didn't consider the fact that `Integer` would cast a float.