Let's say I get some input as a string, and I would like to know whether it contains an Integer
or a Float
.
For example:
input_1 = "100"
input_2 = "100.0123"
Now, let's take into account the following:
input_1.respond_to?(:to_f) # => true
input_1.is_a?(Float) # => false
input_1.is_a?(Fixnum) # => false
input_1.to_f? # => 100.0
input_2.respond_to?(:to_i) # => true
input_2.is_a?(Integer) # => false
input_2.is_a?(Fixnum) # => false
input_2.to_i # => 100
In the cases above, we can't actually determine whether the string contains an Integer
or a Float
. Is there a way in Ruby to determine whether a string contains a Fixnum
or Float
?