Permalink
Browse files

Check modifiable for String `bang' methods

  • Loading branch information...
ksss committed Mar 5, 2017
1 parent d050694 commit eba4b1fd76ae22efc23341f028deb924fdc9401c
Showing with 6 additions and 0 deletions.
  1. +4 −0 mrbgems/mruby-string-ext/mrblib/string.rb
  2. +2 −0 mrblib/string.rb
@@ -95,6 +95,7 @@ def strip
# "hello".lstrip! #=> nil
#
def lstrip!
raise RuntimeError, "can't modify frozen String" if frozen?
s = self.lstrip
(s == self) ? nil : self.replace(s)
end
@@ -111,6 +112,7 @@ def lstrip!
# "hello".rstrip! #=> nil
#
def rstrip!
raise RuntimeError, "can't modify frozen String" if frozen?
s = self.rstrip
(s == self) ? nil : self.replace(s)
end
@@ -123,6 +125,7 @@ def rstrip!
# <code>nil</code> if <i>str</i> was not altered.
#
def strip!
raise RuntimeError, "can't modify frozen String" if frozen?
s = self.strip
(s == self) ? nil : self.replace(s)
end
@@ -183,6 +186,7 @@ def rpartition(sep)
# string #=> "thsa sting"
#
def slice!(arg1, arg2=nil)
raise RuntimeError, "can't modify frozen String" if frozen?
raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
if !arg1.nil? && !arg2.nil?
View
@@ -78,6 +78,7 @@ def gsub(*args, &block)
#
# ISO 15.2.10.5.19
def gsub!(*args, &block)
raise RuntimeError, "can't modify frozen String" if frozen?
str = self.gsub(*args, &block)
return nil if str == self
self.replace(str)
@@ -123,6 +124,7 @@ def sub(*args, &block)
#
# ISO 15.2.10.5.37
def sub!(*args, &block)
raise RuntimeError, "can't modify frozen String" if frozen?
str = self.sub(*args, &block)
return nil if str == self
self.replace(str)

0 comments on commit eba4b1f

Please sign in to comment.