Skip to content

Commit 4702b05

Browse files
authored
Merge pull request #64 from jrbeck/do-not-mutate-strings
Prevent String mutation
2 parents edc4459 + 7004e43 commit 4702b05

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/strip_attributes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def self.strip_record(record, options = {})
5656

5757
def self.strip_string(value, options = {})
5858
return value unless value.is_a?(String)
59-
return value if value.frozen?
59+
value = value.dup
6060

6161
allow_empty = options[:allow_empty]
6262
collapse_spaces = options[:collapse_spaces]

test/strip_attributes_test.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,12 @@ def test_should_strip_and_allow_empty
204204
assert_equal "", record.bang
205205
end
206206

207-
def test_should_skip_frozen_values
208-
record = StripAllMockRecord.new(frozen: " ice ".freeze)
207+
def test_should_not_mutate_values
208+
record = StripAllMockRecord.new(foo: " foo ")
209+
old_value = record.foo
209210
record.valid?
210-
assert_equal " ice ", record.frozen
211+
assert_equal "foo", record.foo
212+
refute_equal old_value, record.foo
211213
end
212214

213215
def test_should_collapse_duplicate_spaces

0 commit comments

Comments
 (0)