mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Check modifiable for String `bang' methods
This commit is contained in:
@@ -95,6 +95,7 @@ class String
|
||||
# "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 @@ class String
|
||||
# "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 @@ class String
|
||||
# <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 @@ class String
|
||||
# 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?
|
||||
|
||||
@@ -78,6 +78,7 @@ class String
|
||||
#
|
||||
# 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 @@ class String
|
||||
#
|
||||
# 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)
|
||||
|
||||
Reference in New Issue
Block a user