Return unfrozen strings from to_sentence

This commit is contained in:
Nicolas Dular 2019-12-28 20:59:50 +01:00
parent 797ae91543
commit 97d428b790
3 changed files with 23 additions and 4 deletions

@ -1,3 +1,15 @@
* `Array#to_sentence` no longer returns a frozen string.
Before:
['one', 'two'].to_sentence.frozen?
# => true
After:
['one', 'two'].to_sentence.frozen?
# => false
*Nicolas Dular*
* When an instance of `ActiveSupport::Duration` is converted to an `iso8601` duration string, if `weeks` are mixed with `date` parts, the `week` part will be converted to days.
This keeps the parser and serializer on the same page.

@ -74,13 +74,13 @@ def to_sentence(options = {})
case length
when 0
""
+""
when 1
"#{self[0]}"
+"#{self[0]}"
when 2
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
+"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
else
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
+"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
end
end

@ -68,6 +68,13 @@ def test_always_returns_string
assert_instance_of String, [ActiveSupport::SafeBuffer.new("one"), "two"].to_sentence
assert_instance_of String, [ActiveSupport::SafeBuffer.new("one"), "two", "three"].to_sentence
end
def test_returns_no_frozen_string
assert_not [].to_sentence.frozen?
assert_not ["one"].to_sentence.frozen?
assert_not ["one", "two"].to_sentence.frozen?
assert_not ["one", "two", "three"].to_sentence.frozen?
end
end
class ToSTest < ActiveSupport::TestCase