summaryrefslogtreecommitdiff
path: root/doc/string
diff options
context:
space:
mode:
authorBurdette Lamar <BurdetteLamar@Yahoo.com>2022-03-24 19:40:58 -0500
committerGitHub <noreply@github.com>2022-03-24 19:40:58 -0500
commit465edb96f08e9405b861559c3f7cea3691be5a77 (patch)
tree588562a361826a23d6ff5c9e9500e6adf93d2bb6 /doc/string
parent343ea9967e4a6b279eed6bd8e81ad0bdc747f254 (diff)
downloadruby-465edb96f08e9405b861559c3f7cea3691be5a77.tar.gz
[DOC] Enhanced RDoc for String (#5707)
Treated: #chomp #chomp! #chop #chop!
Diffstat (limited to 'doc/string')
-rw-r--r--doc/string/chomp.rdoc29
-rw-r--r--doc/string/chop.rdoc16
2 files changed, 45 insertions, 0 deletions
diff --git a/doc/string/chomp.rdoc b/doc/string/chomp.rdoc
new file mode 100644
index 0000000000..b6fb9ff38c
--- /dev/null
+++ b/doc/string/chomp.rdoc
@@ -0,0 +1,29 @@
+Returns a new string copied from +self+, with trailing characters possibly removed:
+
+When +line_sep+ is <tt>"\n"</tt>, removes the last one or two characters
+if they are <tt>"\r"</tt>, <tt>"\n"</tt>, or <tt>"\r\n"</tt>
+(but not <tt>"\n\r"</tt>):
+
+ $/ # => "\n"
+ "abc\r".chomp # => "abc"
+ "abc\n".chomp # => "abc"
+ "abc\r\n".chomp # => "abc"
+ "abc\n\r".chomp # => "abc\n"
+ "тест\r\n".chomp # => "тест"
+ "こんにちは\r\n".chomp # => "こんにちは"
+
+When +line_sep+ is <tt>''</tt> (an empty string),
+removes multiple trailing occurrences of <tt>"\n"</tt> or <tt>"\r\n"</tt>
+(but not <tt>"\r"</tt> or <tt>"\n\r"</tt>):
+
+ "abc\n\n\n".chomp('') # => "abc"
+ "abc\r\n\r\n\r\n".chomp('') # => "abc"
+ "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc"
+ "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r"
+ "abc\r\r\r".chomp('') # => "abc\r\r\r"
+
+When +line_sep+ is neither <tt>"\n"</tt> nor <tt>''</tt>,
+removes a single trailing line separator if there is one:
+
+ 'abcd'.chomp('d') # => "abc"
+ 'abcdd'.chomp('d') # => "abcd"
diff --git a/doc/string/chop.rdoc b/doc/string/chop.rdoc
new file mode 100644
index 0000000000..8ef82f8a49
--- /dev/null
+++ b/doc/string/chop.rdoc
@@ -0,0 +1,16 @@
+Returns a new string copied from +self+, with trailing characters possibly removed.
+
+Removes <tt>"\r\n"</tt> if those are the last two characters.
+
+ "abc\r\n".chop # => "abc"
+ "тест\r\n".chop # => "тест"
+ "こんにちは\r\n".chop # => "こんにちは"
+
+Otherwise removes the last character if it exists.
+
+ 'abcd'.chop # => "abc"
+ 'тест'.chop # => "тес"
+ 'こんにちは'.chop # => "こんにち"
+ ''.chop # => ""
+
+If you only need to remove the newline separator at the end of the string, String#chomp is a better alternative.