From 0ee2c71b034e6168a8c394304325c3cd2a253492 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Tue, 12 Feb 2013 16:07:57 +0900 Subject: Added String#sub/sub! and String#gsub/gsub! --- mrblib/string.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'mrblib') 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 ## -- cgit v1.2.3