summaryrefslogtreecommitdiff
path: root/doc/string/new.rdoc
diff options
context:
space:
mode:
authorBurdette Lamar <BurdetteLamar@Yahoo.com>2022-04-02 14:26:49 -0500
committerGitHub <noreply@github.com>2022-04-02 14:26:49 -0500
commit7be4d900f0e14e6093c726fbc4416560fd56c931 (patch)
tree73f6c240485b3289bd7ea0fbc8b128cd6cd2fc10 /doc/string/new.rdoc
parent4d2623ead2b526fda0dd1e919d9d1dd1d159f57e (diff)
downloadruby-7be4d900f0e14e6093c726fbc4416560fd56c931.tar.gz
[DOC] Enhanced RDoc for String (#5751)
Adds to doc for String.new, also making it compliant with documentation_guide.rdoc. Fixes some broken links in io.c (that I failed to correct yesterday).
Diffstat (limited to 'doc/string/new.rdoc')
-rw-r--r--doc/string/new.rdoc66
1 files changed, 40 insertions, 26 deletions
diff --git a/doc/string/new.rdoc b/doc/string/new.rdoc
index b8dac00856..d955e61c87 100644
--- a/doc/string/new.rdoc
+++ b/doc/string/new.rdoc
@@ -1,36 +1,50 @@
Returns a new \String that is a copy of +string+.
With no arguments, returns the empty string with the Encoding <tt>ASCII-8BIT</tt>:
+
s = String.new
s # => ""
s.encoding # => #<Encoding:ASCII-8BIT>
-With the single \String argument +string+, returns a copy of +string+
-with the same encoding as +string+:
- s = String.new('Que veut dire ça?')
- s # => "Que veut dire ça?"
- s.encoding # => #<Encoding:UTF-8>
-
-Literal strings like <tt>""</tt> or here-documents always use
-{script encoding}[rdoc-ref:Encoding@Script+Encoding], unlike String.new.
-
-With keyword +encoding+, returns a copy of +str+
-with the specified encoding:
- s = String.new(encoding: 'ASCII')
- s.encoding # => #<Encoding:US-ASCII>
- s = String.new('foo', encoding: 'ASCII')
- s.encoding # => #<Encoding:US-ASCII>
-
-Note that these are equivalent:
- s0 = String.new('foo', encoding: 'ASCII')
- s1 = 'foo'.force_encoding('ASCII')
- s0.encoding == s1.encoding # => true
-
-With keyword +capacity+, returns a copy of +str+;
-the given +capacity+ may set the size of the internal buffer,
-which may affect performance:
- String.new(capacity: 1) # => ""
- String.new(capacity: 4096) # => ""
+With optional argument +string+ and no keyword arguments,
+returns a copy of +string+ with the same encoding:
+
+ String.new('foo') # => "foo"
+ String.new('тест') # => "тест"
+ String.new('こんにちは') # => "こんにちは"
+
+(Unlike \String.new,
+a {string literal}[rdoc-ref:syntax/literals.rdoc@String+Literals] like <tt>''</tt> or a
+{here document literal}[rdoc-ref:syntax/literals.rdoc@Here+Document+Literals]
+always has {script encoding}[rdoc-ref:encodings.rdoc@Script+Encoding].)
+
+With optional keyword argument +encoding+, returns a copy of +string+
+with the specified encoding;
+the +encoding+ may be an Encoding object, an encoding name,
+or an encoding name alias:
+
+ String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII>
+ String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII>
+ String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
+
+The given encoding need not be valid for the string's content,
+and that validity is not checked:
+
+ s = String.new('こんにちは', encoding: 'ascii')
+ s.valid_encoding? # => false
+
+But the given +encoding+ itself is checked:
+
+ String.new('foo', encoding: 'bar') # Raises ArgumentError.
+
+With optional keyword argument +capacity+, returns a copy of +string+
+(or an empty string, if +string+ is not given);
+the given +capacity+ is advisory only,
+and may or may not set the size of the internal buffer,
+which may in turn affect performance:
+
+ String.new(capacity: 1)
+ String.new('foo', capacity: 4096)
The +string+, +encoding+, and +capacity+ arguments may all be used together: