summaryrefslogtreecommitdiff
path: root/lib/optparse.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-12 02:49:53 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-12 02:49:53 +0000
commit89bf90a370992e3906bca56d90835682a895bba0 (patch)
treedb25efefd03979afe2c264cab4d5e713aafdb230 /lib/optparse.rb
parent561c253512eb83dba822d50ccc95a90187ab945e (diff)
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/trunk@59312 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/optparse.rb')
-rw-r--r--lib/optparse.rb20
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index b4c44545bb..b99b0a41cd 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1842,7 +1842,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}
@@ -1851,11 +1851,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
}
@@ -1889,10 +1891,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
}