diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/numericality.rb | 11 | ||||
-rw-r--r-- | activemodel/test/cases/validations/numericality_validation_test.rb | 2 |
2 files changed, 9 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 716010e88b..ac8308b0d7 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -57,10 +57,15 @@ module ActiveModel protected def parse_raw_value_as_a_number(raw_value) - begin - Kernel.Float(raw_value) - rescue ArgumentError, TypeError + case raw_value + when /\A0x/ nil + else + begin + Kernel.Float(raw_value) + rescue ArgumentError, TypeError + nil + end end end diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index be620c53fa..963ad6450b 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -18,7 +18,7 @@ class NumericalityValidationTest < ActiveModel::TestCase FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS INTEGERS = [0, 10, -10] + INTEGER_STRINGS BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) } - JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"] + JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"] INFINITY = [1.0/0.0] def test_default_validates_numericality_of |