If you prefer not to use exceptions as part of the logic, you might try this:
class String
def numeric?
!!(self =~ /^-?\d+(\.\d*)?$/)
end
end
Or, if you want it to work across all object classes, replace class String
with class Object
an convert self to a string: !!(self.to_s =~ /^-?\d+(\.\d*)?$/)