summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorBurdette Lamar <BurdetteLamar@Yahoo.com>2025-08-05 09:06:47 -0500
committerGitHub <noreply@github.com>2025-08-05 10:06:47 -0400
commit409da39afbcd927577801be1626193e719f04005 (patch)
treef0071ff9db0cec1cb3e363e9a16884ac39b319e2 /string.c
parentb7f65f01eea8810ac8be64865e3415e634c3633a (diff)
[DOC] Tweaks for String#gsub
Diffstat (limited to 'string.c')
-rw-r--r--string.c35
1 files changed, 31 insertions, 4 deletions
diff --git a/string.c b/string.c
index 68c4f5f1d7..54e719623e 100644
--- a/string.c
+++ b/string.c
@@ -6601,14 +6601,41 @@ rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
* gsub(pattern) {|match| ... } -> new_string
* gsub(pattern) -> enumerator
*
- * Returns a copy of +self+ with all occurrences of the given +pattern+ replaced.
+ * Returns a copy of +self+ with zero or more substrings replaced.
*
- * See {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
+ * Argument +pattern+ may be a string or a Regexp;
+ * argument +replacement+ may be a string or a Hash.
+ * Varying types for the argument values makes this method very versatile.
*
- * Returns an Enumerator if no +replacement+ and no block given.
+ * Below are some simple examples;
+ * for many more examples, see {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
+ *
+ * With arguments +pattern+ and string +replacement+ given,
+ * replaces each matching substring with the given +replacement+ string:
+ *
+ * s = 'abracadabra'
+ * s.gsub('ab', 'AB') # => "ABracadABra"
+ * s.gsub(/[a-c]/, 'X') # => "XXrXXXdXXrX"
+ *
+ * With arguments +pattern+ and hash +replacement+ given,
+ * replaces each matching substring with a value from the given +replacement+ hash,
+ * or removes it:
*
- * Related: String#sub, String#sub!, String#gsub!.
+ * h = {'a' => 'A', 'b' => 'B', 'c' => 'C'}
+ * s.gsub(/[a-c]/, h) # => "ABrACAdABrA" # 'a', 'b', 'c' replaced.
+ * s.gsub(/[a-d]/, h) # => "ABrACAABrA" # 'd' removed.
*
+ * With argument +pattern+ and a block given,
+ * calls the block with each matching substring;
+ * replaces that substring with the block's return value:
+ *
+ * s.gsub(/[a-d]/) {|substring| substring.upcase }
+ * # => "ABrACADABrA"
+ *
+ * With argument +pattern+ and no block given,
+ * returns a new Enumerator.
+ *
+ * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
*/
static VALUE