diff options
-rw-r--r-- | lib/slop/types.rb | 4 | ||||
-rw-r--r-- | test/types_test.rb | 4 |
2 files changed, 7 insertions, 1 deletions
diff --git a/lib/slop/types.rb b/lib/slop/types.rb index c1055ea..c53319c 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -51,8 +51,10 @@ module Slop # Cast the option argument to a Float. class FloatOption < Option + FLOAT_STRING_REGEXP = /\A[+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?\z/.freeze + def call(value) - value =~ /\A[+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?\z/ && value.to_f + value =~ FLOAT_STRING_REGEXP && value.to_f end end diff --git a/test/types_test.rb b/test/types_test.rb index 728b969..d71c737 100644 --- a/test/types_test.rb +++ b/test/types_test.rb @@ -69,15 +69,19 @@ describe Slop::FloatOption do it "parses scientific notations" do assert_equal @scientific_notation_value, @result[:scientific_notation] + @scientific_notation_value = 4E21 @result = @options.parse %W(--scientific-notation #{@scientific_notation_value}) assert_equal @scientific_notation_value, @result[:scientific_notation] + @scientific_notation_value = 4.0e21 @result = @options.parse %W(--scientific-notation #{@scientific_notation_value}) assert_equal @scientific_notation_value, @result[:scientific_notation] + @scientific_notation_value = -4e21 @result = @options.parse %W(--scientific-notation #{@scientific_notation_value}) assert_equal @scientific_notation_value, @result[:scientific_notation] + @scientific_notation_value = 4e-21 @result = @options.parse %W(--scientific-notation #{@scientific_notation_value}) assert_equal @scientific_notation_value, @result[:scientific_notation] |