diff options
| author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-08-09 07:56:52 +0000 |
|---|---|---|
| committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-08-09 07:56:52 +0000 |
| commit | 177cb2c15b305e471d45a44c197a3384fcb2b9ec (patch) | |
| tree | 217ab62971953186127aca11a801c2aa2dc8622a /lib | |
| parent | e1f73c3219a9fb9bc286468c286b582cbca4821e (diff) | |
merge revision(s) 59312: [Backport #13739]
optparse.rb: get rid of eval
* lib/optparse.rb: try Float() and Integer() instead of eval,
which does too much things.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@59530 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/optparse.rb | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index 74d7e8c67c..40425253f4 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1836,7 +1836,7 @@ XXX # # Float number format, and converts to Float. # - float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?" + float = "(?:#{decimal}(?=(.)?)(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?" floatpat = %r"\A[-+]?#{float}\z"io accept(Float, floatpat) {|s,| s.to_f if s} @@ -1845,11 +1845,13 @@ XXX # for float format, and Rational for rational format. # real = "[-+]?(?:#{octal}|#{float})" - accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n| + accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, f, n,| if n Rational(d, n) - elsif s - eval(s) + elsif f + Float(s) + else + Integer(s) end } @@ -1883,10 +1885,14 @@ XXX # integer format, Float for float format. # DecimalNumeric = floatpat # decimal integer is allowed as float also. - accept(DecimalNumeric, floatpat) {|s,| + accept(DecimalNumeric, floatpat) {|s, f| begin - eval(s) - rescue SyntaxError + if f + Float(s) + else + Integer(s) + end + rescue ArgumentError raise OptionParser::InvalidArgument, s end if s } |
