1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/doc/string/new.rdoc
Burdette Lamar c129b6119d
[DOC] Use RDoc inclusions in string.c (#5683)
As @peterzhu2118 and @duerst have pointed out, putting string method's RDoc into doc/ (which allows non-ASCII in examples) makes the "click to toggle source" feature not work for that method.

This PR moves the primary method doc back into string.c, then includes RDoc from doc/string/*.rdoc, and also removes doc/string.rdoc.

The affected methods are:

    ::new
    #bytes
    #each_byte
    #each_line
    #split

The call-seq is in string.c because it works there; it did not work when the call-seq is in doc/string/*.rdoc.

This PR also updates the relevant guidance in doc/documentation_guide.rdoc.
2022-03-21 14:58:00 -05:00

37 lines
1.2 KiB
Text

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
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) # => ""
The +string+, +encoding+, and +capacity+ arguments may all be used together:
String.new('hello', encoding: 'UTF-8', capacity: 25)