From faff37da57ac2e760704945c9e1f946b850bdad8 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Mon, 7 Mar 2022 12:58:29 -0600 Subject: [DOC] Enhanced RDoc for String #tr and #tr! (#5626) --- string.c | 68 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/string.c b/string.c index e8ac779053..67ea072407 100644 --- a/string.c +++ b/string.c @@ -8007,11 +8007,11 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag) /* * call-seq: - * str.tr!(from_str, to_str) -> str or nil + * tr!(replaceables, replacements) -> self or nil + * + * Like String#tr, but translates +self+ in place. + * Returns +self+ if any changes were made, +nil+ otherwise. * - * Translates str in place, using the same rules as - * String#tr. Returns str, or nil if no changes - * were made. */ static VALUE @@ -8023,37 +8023,51 @@ rb_str_tr_bang(VALUE str, VALUE src, VALUE repl) /* * call-seq: - * str.tr(from_str, to_str) => new_str + * tr(replaceables, replacements) -> new_string + * + * Returns a copy of +self+ with each character specified by string +replaceables+ + * translated to the corresponding character in string +replacements+. + * The correspondence is _positional_: + * + * - Each occurrence of the first character in +replaceables+ + * is translated to the first character in +replacements+. + * - Each occurrence of the second character in +replaceables+ + * is translated to the second character in +replacements+. + * - And so on. + * + * Example: + * + * 'hello'.tr('el', 'ip') #=> "hippo" + * + * If +replacements+ is shorter than +replaceables+, + * it is implicitly padded with its own last character: + * + * 'hello'.tr('aeiou', '-') # => "h-ll-" + * 'hello'.tr('aeiou', 'AA-') # => "hAll-" * - * Returns a copy of +str+ with the characters in +from_str+ replaced by the - * corresponding characters in +to_str+. If +to_str+ is shorter than - * +from_str+, it is padded with its last character in order to maintain the - * correspondence. + * Three characters may get special treatment: * - * "hello".tr('el', 'ip') #=> "hippo" - * "hello".tr('aeiou', '*') #=> "h*ll*" - * "hello".tr('aeiou', 'AA*') #=> "hAll*" + * - A caret ('^') in +replaceables+ + * operates as a "not" operator for the following characters in +replaceables+: * - * Both strings may use the c1-c2 notation to denote ranges of - * characters, and +from_str+ may start with a ^, which denotes - * all characters except those listed. + * 'hello'.tr('^aeiou', '-') # => "-e--o" + * '^hello'.tr('ae^iou', '-') # => "-h-ll-" * - * "hello".tr('a-y', 'b-z') #=> "ifmmp" - * "hello".tr('^aeiou', '*') #=> "*e**o" + * - A hyphen ('-') embedded in a 3-character +replaceables+ or +replacements+ + * defines a range of characters instead of a plain string of characters: * - * The backslash character \\ can be used to escape - * ^ or - and is otherwise ignored unless it - * appears at the end of a range or the end of the +from_str+ or +to_str+: + * 'ibm'.tr('b-z', 'a-z') # => "hal" * - * "hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld" - * "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld" + * - A backslash ('\') before a caret, a hyphen, or another backslash + * operates as an escape character: * - * "hello\r\nworld".tr("\r", "") #=> "hello\nworld" - * "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold" - * "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld" + * 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret. + * 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen. + * 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash. + * "hello\r\nworld".tr("\r", "") # => "hello\nworld" + * "hello\r\nworld".tr("\\r", "") # => "hello\r\nwold" + * "hello\r\nworld".tr("\\\r", "") # => "hello\nworld" * - * "X['\\b']".tr("X\\", "") #=> "['b']" - * "X['\\b']".tr("X-\\]", "") #=> "'b'" */ static VALUE -- cgit v1.2.3