title: Converting numeric string to numeric in Ruby
body: <p>I want a method like to_numeric(str) which convert numeric string 'str' into its numeric form else return nil. By numeric form if string is in integer method should return integer and it string is in float it should return float.</p>
  
  <p>I have tried with following code. It works fine but need better solution if possible.</p>
  
  <pre><code>def to_numeric(str)
    Integer(str)
  rescue
    Float(str) if Float(str) rescue nil
  end
  </code></pre>
  
  <p>One important thing I forgot to mention is "I don't know the type of my input".</p>
  
  <p>My use case:</p>
  
  <pre><code>arr = [1, 1.5, 2, 2.5, 4]
  some_input = get_input_from_some_source
  
  if arr.include?(to_numeric(some_input))
    # do something
  end
  </code></pre>