Skip to content

Commit bb17d78

Browse files
Ensure {down,up}case_first returns non-frozen string
Prior to this commit, `String#downcase_first` and `String#upcase_first` would return a frozen string when called on an empty string: ```ruby # BEFORE "foo".downcase_first.frozen? # => false "".downcase_first.frozen? # => true # AFTER "foo".downcase_first.frozen? # => false "".downcase_first.frozen? # => false ```
1 parent 2a986b7 commit bb17d78

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

activesupport/lib/active_support/inflector/methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def humanize(lower_case_and_underscored_word, capitalize: true, keep_id_suffix:
164164
# upcase_first('w') # => "W"
165165
# upcase_first('') # => ""
166166
def upcase_first(string)
167-
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ""
167+
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : +""
168168
end
169169

170170
# Converts the first character in the string to lowercase.
@@ -173,7 +173,7 @@ def upcase_first(string)
173173
# downcase_first('I') # => "i"
174174
# downcase_first('') # => ""
175175
def downcase_first(string)
176-
string.length > 0 ? string[0].downcase.concat(string[1..-1]) : ""
176+
string.length > 0 ? string[0].downcase.concat(string[1..-1]) : +""
177177
end
178178

179179
# Capitalizes all the words and replaces some characters in the string to

activesupport/test/core_ext/string_ext_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def test_downcase_first_with_one_char
100100

101101
def test_downcase_first_with_empty_string
102102
assert_equal "", "".downcase_first
103+
assert_not_predicate "".downcase_first, :frozen?
103104
end
104105

105106
def test_upcase_first
@@ -112,6 +113,7 @@ def test_upcase_first_with_one_char
112113

113114
def test_upcase_first_with_empty_string
114115
assert_equal "", "".upcase_first
116+
assert_not_predicate "".upcase_first, :frozen?
115117
end
116118

117119
def test_camelize

0 commit comments

Comments
 (0)