[ruby/optparse] Prefer Proc over Method

The performances are: block > proc > method object.

https://github.com/ruby/optparse/commit/9ec5d1d582
This commit is contained in:
Nobuyoshi Nakada 2025-09-15 22:10:14 +09:00 committed by git
parent d89b453392
commit 70210acab0

View File

@ -1855,7 +1855,7 @@ XXX
#
def permute!(argv = default_argv, **keywords)
nonopts = []
order!(argv, **keywords, &nonopts.method(:<<))
order!(argv, **keywords) {|nonopt| nonopts << nonopt}
argv[0, 0] = nonopts
argv
end
@ -1908,13 +1908,16 @@ XXX
single_options, *long_options = *args
result = {}
setter = (symbolize_names ?
->(name, val) {result[name.to_sym] = val}
: ->(name, val) {result[name] = val})
single_options.scan(/(.)(:)?/) do |opt, val|
if val
result[opt] = nil
setter[opt, nil]
define("-#{opt} VAL")
else
result[opt] = false
setter[opt, false]
define("-#{opt}")
end
end if single_options
@ -1923,16 +1926,16 @@ XXX
arg, desc = arg.split(';', 2)
opt, val = arg.split(':', 2)
if val
result[opt] = val.empty? ? nil : val
setter[opt, (val unless val.empty?)]
define("--#{opt}=#{result[opt] || "VAL"}", *[desc].compact)
else
result[opt] = false
setter[opt, false]
define("--#{opt}", *[desc].compact)
end
end
parse_in_order(argv, result.method(:[]=), **keywords)
symbolize_names ? result.transform_keys(&:to_sym) : result
parse_in_order(argv, setter, **keywords)
result
end
#
@ -1982,7 +1985,7 @@ XXX
visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
}
exc = ambiguous ? AmbiguousOption : InvalidOption
raise exc.new(opt, additional: self.method(:additional_message).curry[typ])
raise exc.new(opt, additional: proc {|o| additional_message(typ, o)})
end
private :complete
@ -2273,9 +2276,10 @@ XXX
argv
end
DIR = File.join(__dir__, '')
def self.filter_backtrace(array)
unless $DEBUG
array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
array.delete_if {|bt| bt.start_with?(DIR)}
end
array
end