diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-02-11 23:39:53 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-02-11 23:39:53 -0800 |
| commit | 8578cd622ba6644a0421bcb35e44e0f7b2927a54 (patch) | |
| tree | a691ece98278c512c7fbaffa9f7f0180c68a9428 /mrblib/string.rb | |
| parent | 4b9853a1a671550d252aed3364abf3de7da54420 (diff) | |
| parent | 0ee2c71b034e6168a8c394304325c3cd2a253492 (diff) | |
| download | mruby-8578cd622ba6644a0421bcb35e44e0f7b2927a54.tar.gz mruby-8578cd622ba6644a0421bcb35e44e0f7b2927a54.zip | |
Merge pull request #839 from masuidrive/string_sub_gsub
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 ## |
