Skip to content

Commit

Permalink
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/string.rb
Original file line number Diff line number Diff line change
@@ -2680,4 +2680,12 @@ def dump
str += ".force_encoding(\"#{encoding}\")" unless encoding.ascii_compatible?
s.replace(str)
end

def -@
frozen? ? self : dup.freeze
end

def +@
frozen? ? dup : self
end
end
19 changes: 19 additions & 0 deletions spec/ruby/core/string/minus_prefix_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe 'String#-@' do
it 'returns self if the String is frozen' do
input = 'foo'.freeze
output = -input

output.equal?(input).should == true
output.frozen?.should == true
end

it 'returns a frozen copy if the String is not frozen' do
input = 'foo'
output = -input

output.frozen?.should == true
output.should == 'foo'
end
end
18 changes: 18 additions & 0 deletions spec/ruby/core/string/plus_prefix_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe 'String#+@' do
it 'returns an unfrozen copy of a frozen String' do
input = 'foo'.freeze
output = +input

output.frozen?.should == false
output.should == 'foo'
end

it 'returns self if the String is not frozen' do
input = 'foo'
output = +input

output.equal?(input).should == true
end
end

0 comments on commit 0d7dd0c

Please sign in to comment.