Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 54cb8c2

Browse files
committed
Merge pull request #394 from mvz/test-hashes
Use old-style hashes to pass keyword arguments
1 parent db9e3b7 commit 54cb8c2

File tree

1 file changed

+19
-52
lines changed

1 file changed

+19
-52
lines changed

lib/rspec/support/encoded_string.rb

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,11 @@ class EncodedString
55
# Reduce allocations by storing constants.
66
UTF_8 = "UTF-8"
77
US_ASCII = "US-ASCII"
8-
#
9-
# In MRI 2.1 'invalid: :replace' changed to also replace an invalid byte sequence
10-
# see https://github.com/ruby/ruby/blob/v2_1_0/NEWS#L176
11-
# https://www.ruby-forum.com/topic/6861247
12-
# https://twitter.com/nalsh/status/553413844685438976
13-
#
14-
# For example, given:
15-
# "\x80".force_encoding("Emacs-Mule").encode(:invalid => :replace).bytes.to_a
16-
#
17-
# On MRI 2.1 or above: 63 # '?'
18-
# else : 128 # "\x80"
19-
#
8+
209
# Ruby's default replacement string is:
2110
# U+FFFD ("\xEF\xBF\xBD"), for Unicode encoding forms, else
2211
# ? ("\x3F")
2312
REPLACE = "?"
24-
ENCODE_UNCONVERTABLE_BYTES = {
25-
:invalid => :replace,
26-
:undef => :replace,
27-
:replace => REPLACE
28-
}
29-
ENCODE_NO_CONVERTER = {
30-
:invalid => :replace,
31-
:replace => REPLACE
32-
}
3313

3414
def initialize(string, encoding=nil)
3515
@encoding = encoding
@@ -112,40 +92,27 @@ def matching_encoding(string)
11292
string = remove_invalid_bytes(string)
11393
string.encode(@encoding)
11494
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
115-
encode_unconvertable_bytes(string)
95+
# Originally defined as a constant to avoid uneeded allocations, this hash must
96+
# be defined inline (without {}) to avoid warnings on Ruby 2.7
97+
#
98+
# In MRI 2.1 'invalid: :replace' changed to also replace an invalid byte sequence
99+
# see https://github.com/ruby/ruby/blob/v2_1_0/NEWS#L176
100+
# https://www.ruby-forum.com/topic/6861247
101+
# https://twitter.com/nalsh/status/553413844685438976
102+
#
103+
# For example, given:
104+
# "\x80".force_encoding("Emacs-Mule").encode(:invalid => :replace).bytes.to_a
105+
#
106+
# On MRI 2.1 or above: 63 # '?'
107+
# else : 128 # "\x80"
108+
#
109+
string.encode(@encoding, :invalid => :replace, :undef => :replace, :replace => REPLACE)
116110
rescue Encoding::ConverterNotFoundError
117-
encode_no_converter(string.dup.force_encoding(@encoding))
111+
# Originally defined as a constant to avoid uneeded allocations, this hash must
112+
# be defined inline (without {}) to avoid warnings on Ruby 2.7
113+
string.dup.force_encoding(@encoding).encode(:invalid => :replace, :replace => REPLACE)
118114
end
119115

120-
private
121-
122-
# On Ruby 2.7.0 keyword arguments mixed with conventional cause a warning to
123-
# be issued requiring us to be explicit by using a ** to pass the hash as
124-
# keyword arguments. Any keyword argument supporting Ruby supports this.
125-
if RubyFeatures.kw_args_supported?
126-
# Note on non keyword supporting Ruby ** causes a syntax error hence
127-
# we must use eval. To be removed in RSpec 4.
128-
binding.eval(<<-CODE, __FILE__, __LINE__)
129-
def encode_unconvertable_bytes(string)
130-
string.encode(@encoding, **ENCODE_UNCONVERTABLE_BYTES)
131-
end
132-
133-
def encode_no_converter(string)
134-
string.encode(**ENCODE_NO_CONVERTER)
135-
end
136-
CODE
137-
else
138-
def encode_unconvertable_bytes(string)
139-
string.encode(@encoding, ENCODE_UNCONVERTABLE_BYTES)
140-
end
141-
142-
def encode_no_converter(string)
143-
string.encode(ENCODE_NO_CONVERTER)
144-
end
145-
end
146-
147-
public
148-
149116
# Prevents raising ArgumentError
150117
if String.method_defined?(:scrub)
151118
# https://github.com/ruby/ruby/blob/eeb05e8c11/doc/NEWS-2.1.0#L120-L123

0 commit comments

Comments
 (0)