diff options
| -rw-r--r-- | mrblib/string.rb | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index 37441ec98..f22071b36 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -159,16 +159,27 @@ class String anum = args.size if anum == 2 pos, value = args - if pos.kind_of? String + case pos + when String posnum = self.index(pos) if posnum b = self[0, posnum.to_i] a = self[(posnum + pos.length)..-1] self.replace([b, value, a].join('')) - return value else raise IndexError, "string not matched" end + when Range + head = pos.begin + tail = pos.end + tail += self.length if tail < 0 + if pos.exclude_end? + tail -= 1 + end + if tail < 0 || tail > self.length + raise IndexError, "index #{args[0]} out of string" + end + return self[head, tail-head]=value else pos += self.length if pos < 0 if pos < 0 || pos > self.length @@ -177,8 +188,8 @@ class String b = self[0, pos.to_i] a = self[pos + 1..-1] self.replace([b, value, a].join('')) - return value end + return value elsif anum == 3 pos, len, value = args pos += self.length if pos < 0 |
