How can I check if a value is a number?

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).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)