Skip to content

Commit 70210ac

Browse files
nobumatzbot
authored andcommitted
[ruby/optparse] Prefer Proc over Method
The performances are: block > proc > method object. ruby/optparse@9ec5d1d582
1 parent d89b453 commit 70210ac

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/optparse.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ def permute(*argv, **keywords)
18551855
#
18561856
def permute!(argv = default_argv, **keywords)
18571857
nonopts = []
1858-
order!(argv, **keywords, &nonopts.method(:<<))
1858+
order!(argv, **keywords) {|nonopt| nonopts << nonopt}
18591859
argv[0, 0] = nonopts
18601860
argv
18611861
end
@@ -1908,13 +1908,16 @@ def getopts(*args, symbolize_names: false, **keywords)
19081908
single_options, *long_options = *args
19091909

19101910
result = {}
1911+
setter = (symbolize_names ?
1912+
->(name, val) {result[name.to_sym] = val}
1913+
: ->(name, val) {result[name] = val})
19111914

19121915
single_options.scan(/(.)(:)?/) do |opt, val|
19131916
if val
1914-
result[opt] = nil
1917+
setter[opt, nil]
19151918
define("-#{opt} VAL")
19161919
else
1917-
result[opt] = false
1920+
setter[opt, false]
19181921
define("-#{opt}")
19191922
end
19201923
end if single_options
@@ -1923,16 +1926,16 @@ def getopts(*args, symbolize_names: false, **keywords)
19231926
arg, desc = arg.split(';', 2)
19241927
opt, val = arg.split(':', 2)
19251928
if val
1926-
result[opt] = val.empty? ? nil : val
1929+
setter[opt, (val unless val.empty?)]
19271930
define("--#{opt}=#{result[opt] || "VAL"}", *[desc].compact)
19281931
else
1929-
result[opt] = false
1932+
setter[opt, false]
19301933
define("--#{opt}", *[desc].compact)
19311934
end
19321935
end
19331936

1934-
parse_in_order(argv, result.method(:[]=), **keywords)
1935-
symbolize_names ? result.transform_keys(&:to_sym) : result
1937+
parse_in_order(argv, setter, **keywords)
1938+
result
19361939
end
19371940

19381941
#
@@ -1982,7 +1985,7 @@ def complete(typ, opt, icase = false, *pat) # :nodoc:
19821985
visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
19831986
}
19841987
exc = ambiguous ? AmbiguousOption : InvalidOption
1985-
raise exc.new(opt, additional: self.method(:additional_message).curry[typ])
1988+
raise exc.new(opt, additional: proc {|o| additional_message(typ, o)})
19861989
end
19871990
private :complete
19881991

@@ -2273,9 +2276,10 @@ def recover(argv)
22732276
argv
22742277
end
22752278

2279+
DIR = File.join(__dir__, '')
22762280
def self.filter_backtrace(array)
22772281
unless $DEBUG
2278-
array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
2282+
array.delete_if {|bt| bt.start_with?(DIR)}
22792283
end
22802284
array
22812285
end

0 commit comments

Comments
 (0)