Skip to content

Commit be9b64d

Browse files
[DOC] StringIO doc enhancements (#33)
Treated: - ::new - ::open - #string - #string= - #close - #close_read - #close_write - #closed? - #closed_read? - #closed_write? - #eof?
1 parent 16847fe commit be9b64d

File tree

1 file changed

+101
-42
lines changed

1 file changed

+101
-42
lines changed

ext/stringio/stringio.c

+101-42
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,20 @@ strio_s_allocate(VALUE klass)
257257
}
258258

259259
/*
260-
* call-seq: StringIO.new(string=""[, mode])
260+
* call-seq:
261+
* StringIO.new(string = '', mode = 'r+') -> new_stringio
262+
*
263+
* Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
264+
*
265+
* Returns a new \StringIO instance formed from +string+ and +mode+;
266+
* see {Access Modes}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Access+Modes]:
267+
*
268+
* strio = StringIO.new # => #<StringIO>
269+
* strio.close
261270
*
262-
* Creates new StringIO instance from with _string_ and _mode_.
271+
* The instance should be closed when no longer needed.
272+
*
273+
* Related: StringIO.open (accepts block; closes automatically).
263274
*/
264275
static VALUE
265276
strio_initialize(int argc, VALUE *argv, VALUE self)
@@ -392,11 +403,26 @@ strio_finalize(VALUE self)
392403
}
393404

394405
/*
395-
* call-seq: StringIO.open(string=""[, mode]) {|strio| ...}
406+
* call-seq:
407+
* StringIO.open(string = '', mode = 'r+') {|strio| ... }
408+
*
409+
* Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
410+
*
411+
* Creates a new \StringIO instance formed from +string+ and +mode+;
412+
* see {Access Modes}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Access+Modes].
396413
*
397-
* Equivalent to StringIO.new except that when it is called with a block, it
398-
* yields with the new instance and closes it, and returns the result which
399-
* returned from the block.
414+
* With no block, returns the new instance:
415+
*
416+
* strio = StringIO.open # => #<StringIO>
417+
*
418+
* With a block, calls the block with the new instance
419+
* and returns the block's value;
420+
* closes the instance on block exit.
421+
*
422+
* StringIO.open {|strio| p strio }
423+
* # => #<StringIO>
424+
*
425+
* Related: StringIO.new.
400426
*/
401427
static VALUE
402428
strio_s_open(int argc, VALUE *argv, VALUE klass)
@@ -482,9 +508,23 @@ strio_unimpl(int argc, VALUE *argv, VALUE self)
482508
}
483509

484510
/*
485-
* call-seq: strio.string -> string
511+
* call-seq:
512+
* string -> string
513+
*
514+
* Returns underlying string:
515+
*
516+
* StringIO.open('foo') do |strio|
517+
* p strio.string
518+
* strio.string = 'bar'
519+
* p strio.string
520+
* end
486521
*
487-
* Returns underlying String object, the subject of IO.
522+
* Output:
523+
*
524+
* "foo"
525+
* "bar"
526+
*
527+
* Related: StringIO#string= (assigns the underlying string).
488528
*/
489529
static VALUE
490530
strio_get_string(VALUE self)
@@ -494,9 +534,23 @@ strio_get_string(VALUE self)
494534

495535
/*
496536
* call-seq:
497-
* strio.string = string -> string
537+
* string = other_string -> other_string
538+
*
539+
* Assigns the underlying string as +other_string+, and sets position to zero;
540+
* returns +other_string+:
541+
*
542+
* StringIO.open('foo') do |strio|
543+
* p strio.string
544+
* strio.string = 'bar'
545+
* p strio.string
546+
* end
498547
*
499-
* Changes underlying String object, the subject of IO.
548+
* Output:
549+
*
550+
* "foo"
551+
* "bar"
552+
*
553+
* Related: StringIO#string (returns the underlying string).
500554
*/
501555
static VALUE
502556
strio_set_string(VALUE self, VALUE string)
@@ -514,10 +568,13 @@ strio_set_string(VALUE self, VALUE string)
514568

515569
/*
516570
* call-seq:
517-
* strio.close -> nil
571+
* close -> nil
572+
*
573+
* Closes +self+ for both reading and writing.
574+
*
575+
* Raises IOError if reading or writing is attempted.
518576
*
519-
* Closes a StringIO. The stream is unavailable for any further data
520-
* operations; an +IOError+ is raised if such an attempt is made.
577+
* Related: StringIO#close_read, StringIO#close_write.
521578
*/
522579
static VALUE
523580
strio_close(VALUE self)
@@ -529,10 +586,13 @@ strio_close(VALUE self)
529586

530587
/*
531588
* call-seq:
532-
* strio.close_read -> nil
589+
* close_read -> nil
533590
*
534-
* Closes the read end of a StringIO. Will raise an +IOError+ if the
535-
* receiver is not readable.
591+
* Closes +self+ for reading; closed-write setting remains unchanged.
592+
*
593+
* Raises IOError if reading is attempted.
594+
*
595+
* Related: StringIO#close, StringIO#close_write.
536596
*/
537597
static VALUE
538598
strio_close_read(VALUE self)
@@ -547,10 +607,13 @@ strio_close_read(VALUE self)
547607

548608
/*
549609
* call-seq:
550-
* strio.close_write -> nil
610+
* close_write -> nil
611+
*
612+
* Closes +self+ for writing; closed-read setting remains unchanged.
613+
*
614+
* Raises IOError if writing is attempted.
551615
*
552-
* Closes the write end of a StringIO. Will raise an +IOError+ if the
553-
* receiver is not writeable.
616+
* Related: StringIO#close, StringIO#close_read.
554617
*/
555618
static VALUE
556619
strio_close_write(VALUE self)
@@ -565,9 +628,10 @@ strio_close_write(VALUE self)
565628

566629
/*
567630
* call-seq:
568-
* strio.closed? -> true or false
631+
* closed? -> true or false
569632
*
570-
* Returns +true+ if the stream is completely closed, +false+ otherwise.
633+
* Returns +true+ if +self+ is closed for both reading and writing,
634+
* +false+ otherwise.
571635
*/
572636
static VALUE
573637
strio_closed(VALUE self)
@@ -579,9 +643,9 @@ strio_closed(VALUE self)
579643

580644
/*
581645
* call-seq:
582-
* strio.closed_read? -> true or false
646+
* closed_read? -> true or false
583647
*
584-
* Returns +true+ if the stream is not readable, +false+ otherwise.
648+
* Returns +true+ if +self+ is closed for reading, +false+ otherwise.
585649
*/
586650
static VALUE
587651
strio_closed_read(VALUE self)
@@ -593,9 +657,9 @@ strio_closed_read(VALUE self)
593657

594658
/*
595659
* call-seq:
596-
* strio.closed_write? -> true or false
660+
* closed_write? -> true or false
597661
*
598-
* Returns +true+ if the stream is not writable, +false+ otherwise.
662+
* Returns +true+ if +self+ is closed for writing, +false+ otherwise.
599663
*/
600664
static VALUE
601665
strio_closed_write(VALUE self)
@@ -615,11 +679,14 @@ strio_to_read(VALUE self)
615679

616680
/*
617681
* call-seq:
618-
* strio.eof -> true or false
619-
* strio.eof? -> true or false
682+
* eof? -> true or false
620683
*
621-
* Returns true if the stream is at the end of the data (underlying string).
622-
* The stream must be opened for reading or an +IOError+ will be raised.
684+
* Returns +true+ if positioned at end-of-stream, +false+ otherwise;
685+
* see {Position}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Position].
686+
*
687+
* Raises IOError if the stream is not opened for reading.
688+
*
689+
* StreamIO#eof is an alias for StreamIO#eof?.
623690
*/
624691
static VALUE
625692
strio_eof(VALUE self)
@@ -1751,24 +1818,16 @@ strio_set_encoding_by_bom(VALUE self)
17511818
}
17521819

17531820
/*
1754-
* Pseudo I/O on String object, with interface corresponding to IO.
1821+
* \IO streams for strings, with access similar to
1822+
* {IO}[https://docs.ruby-lang.org/en/master/IO.html];
1823+
* see {IO Streams}[https://docs.ruby-lang.org/en/master/io_streams_rdoc.html].
17551824
*
1756-
* Commonly used to simulate <code>$stdio</code> or <code>$stderr</code>
1825+
* === About the Examples
17571826
*
1758-
* === Examples
1827+
* Examples on this page assume that \StringIO has been required:
17591828
*
17601829
* require 'stringio'
17611830
*
1762-
* # Writing stream emulation
1763-
* io = StringIO.new
1764-
* io.puts "Hello World"
1765-
* io.string #=> "Hello World\n"
1766-
*
1767-
* # Reading stream emulation
1768-
* io = StringIO.new "first\nsecond\nlast\n"
1769-
* io.getc #=> "f"
1770-
* io.gets #=> "irst\n"
1771-
* io.read #=> "second\nlast\n"
17721831
*/
17731832
void
17741833
Init_stringio(void)

0 commit comments

Comments
 (0)