Skip to content

Commit b37025c

Browse files
committed
Replace =~ or match by match? when we don't need the MatchData object. match? function is new from 2.4
Add CHANGELOG.md
1 parent 20a6d3f commit b37025c

File tree

8 files changed

+13
-12
lines changed

8 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44
* Your contribution here.
5+
* [#2015](https://github.com/ruby-grape/grape/pull/2014): Reduce MatchData allocation - [@ericproulx](https://github.com/ericproulx).
56
* [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce total allocated arrays - [@ericproulx](https://github.com/ericproulx).
67
* [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).
78

lib/grape/middleware/versioner/header.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def strict_version_vendor_accept_header_presence_check
6363

6464
def an_accept_header_with_version_and_vendor_is_present?
6565
header.qvalues.keys.any? do |h|
66-
VENDOR_VERSION_HEADER_REGEX =~ h.sub('application/', '')
66+
VENDOR_VERSION_HEADER_REGEX.match?(h.sub('application/', ''))
6767
end
6868
end
6969

lib/grape/middleware/versioner/parse_media_type_patch.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
module Rack
44
module Accept
55
module Header
6+
ALLOWED_CHARACTERS = %r{^([a-z*]+)\/([a-z0-9*\&\^\-_#\$!.+]+)(?:;([a-z0-9=;]+))?$}.freeze
67
class << self
78
# Corrected version of https://github.com/mjackson/rack-accept/blob/master/lib/rack/accept/header.rb#L40-L44
89
def parse_media_type(media_type)
910
# see http://tools.ietf.org/html/rfc6838#section-4.2 for allowed characters in media type names
10-
m = media_type.to_s.match(%r{^([a-z*]+)\/([a-z0-9*\&\^\-_#\$!.+]+)(?:;([a-z0-9=;]+))?$})
11+
m = media_type.match(ALLOWED_CHARACTERS)
1112
m ? [m[1], m[2], m[3] || ''] : []
1213
end
1314
end

lib/grape/path.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def uses_path_versioning?
4242
end
4343

4444
def namespace?
45-
namespace && namespace.to_s =~ /^\S/ && namespace != '/'
45+
namespace&.match?(/^\S/) && namespace != '/'
4646
end
4747

4848
def path?
49-
raw_path && raw_path.to_s =~ /^\S/ && raw_path != '/'
49+
raw_path&.match?(/^\S/) && raw_path != '/'
5050
end
5151

5252
def suffix

lib/grape/router/route.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def method_missing(method_id, *arguments)
3131
end
3232

3333
def respond_to_missing?(method_id, _)
34-
ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s)
34+
ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
3535
end
3636

3737
%i[
@@ -67,7 +67,6 @@ def initialize(method, pattern, **options)
6767
method_s = method.to_s
6868
method_upcase = Grape::Http::Headers.find_supported_method(method_s) || method_s.upcase
6969

70-
@suffix = options[:suffix]
7170
@options = options.merge(method: method_upcase)
7271
@pattern = Pattern.new(pattern, **options)
7372
@translator = AttributeTranslator.new(**options, request_method: method_upcase)

lib/grape/validations/types/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def call(input)
2020
return input if coerced?(input)
2121

2222
# Allow nulls and blank strings
23-
return if input.nil? || input =~ /^\s*$/
23+
return if input.nil? || input.match?(/^\s*$/)
2424
JSON.parse(input, symbolize_names: true)
2525
end
2626

lib/grape/validations/validators/regexp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Validations
55
class RegexpValidator < Base
66
def validate_param!(attr_name, params)
77
return unless params.respond_to?(:key?) && params.key?(attr_name)
8-
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || (param.to_s =~ (options_key?(:value) ? @option[:value] : @option)) }
8+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.match?((options_key?(:value) ? @option[:value] : @option)) }
99
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp))
1010
end
1111
end

spec/grape/path_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ module Grape
8787
describe '#namespace?' do
8888
it 'is false when the namespace is nil' do
8989
path = Path.new(anything, nil, anything)
90-
expect(path.namespace?).to be nil
90+
expect(path.namespace?).to be_falsey
9191
end
9292

9393
it 'is false when the namespace starts with whitespace' do
9494
path = Path.new(anything, ' /foo', anything)
95-
expect(path.namespace?).to be nil
95+
expect(path.namespace?).to be_falsey
9696
end
9797

9898
it 'is false when the namespace is the root path' do
@@ -109,12 +109,12 @@ module Grape
109109
describe '#path?' do
110110
it 'is false when the path is nil' do
111111
path = Path.new(nil, anything, anything)
112-
expect(path.path?).to be nil
112+
expect(path.path?).to be_falsey
113113
end
114114

115115
it 'is false when the path starts with whitespace' do
116116
path = Path.new(' /foo', anything, anything)
117-
expect(path.path?).to be nil
117+
expect(path.path?).to be_falsey
118118
end
119119

120120
it 'is false when the path is the root path' do

0 commit comments

Comments
 (0)