Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added String#-@ and String#+@
Fixes #3608
  • Loading branch information
Yorick Peterse committed Feb 12, 2016
1 parent b372185 commit 0d7dd0c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/string.rb
Expand Up @@ -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
@@ -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
@@ -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.