diff options
| author | Yuichiro MASUI <[email protected]> | 2013-02-12 16:07:57 +0900 |
|---|---|---|
| committer | Yuichiro MASUI <[email protected]> | 2013-02-12 16:10:35 +0900 |
| commit | 0ee2c71b034e6168a8c394304325c3cd2a253492 (patch) | |
| tree | 3dd9da01e5f5ce6b029a5a881acc3ecd5933b36a /mrblib/string.rb | |
| parent | 3e021b8e858194aba692912af26bc2a81e0b67ed (diff) | |
| download | mruby-0ee2c71b034e6168a8c394304325c3cd2a253492.tar.gz mruby-0ee2c71b034e6168a8c394304325c3cd2a253492.zip | |
Added String#sub/sub! and String#gsub/gsub!
Diffstat (limited to 'mrblib/string.rb')
| -rw-r--r-- | mrblib/string.rb | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index 43dda16e5..9b11bb2e5 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -28,14 +28,13 @@ class String # # ISO 15.2.10.5.18 def gsub(*args, &block) - unless (args.size == 1 && block) || args.size == 2 + if args.size == 2 + split(args[0]).join(args[1]) + elsif args.size == 1 && block + split(args[0]).join(block.call(args[0])) + else raise ArgumentError, "wrong number of arguments" end - - ### *** TODO *** ### - unless Object.const_defined?(:Regexp) - raise NotImplementedError, "gsub not available (yet)" - end end ## @@ -76,14 +75,13 @@ class String # # ISO 15.2.10.5.36 def sub(*args, &block) - unless (args.size == 1 && block) || args.size == 2 + if args.size == 2 + split(args[0], 2).join(args[1]) + elsif args.size == 1 && block + split(args[0], 2).join(block.call(args[0])) + else raise ArgumentError, "wrong number of arguments" end - - ### *** TODO *** ### - unless Object.const_defined?(:Regexp) - raise NotImplementedError, "sub not available (yet)" - end end ## |
