@@ -257,9 +257,20 @@ strio_s_allocate(VALUE klass)
257
257
}
258
258
259
259
/*
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
261
270
*
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).
263
274
*/
264
275
static VALUE
265
276
strio_initialize (int argc , VALUE * argv , VALUE self )
@@ -392,11 +403,26 @@ strio_finalize(VALUE self)
392
403
}
393
404
394
405
/*
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].
396
413
*
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.
400
426
*/
401
427
static VALUE
402
428
strio_s_open (int argc , VALUE * argv , VALUE klass )
@@ -482,9 +508,23 @@ strio_unimpl(int argc, VALUE *argv, VALUE self)
482
508
}
483
509
484
510
/*
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
486
521
*
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).
488
528
*/
489
529
static VALUE
490
530
strio_get_string (VALUE self )
@@ -494,9 +534,23 @@ strio_get_string(VALUE self)
494
534
495
535
/*
496
536
* 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
498
547
*
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).
500
554
*/
501
555
static VALUE
502
556
strio_set_string (VALUE self , VALUE string )
@@ -514,10 +568,13 @@ strio_set_string(VALUE self, VALUE string)
514
568
515
569
/*
516
570
* 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.
518
576
*
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.
521
578
*/
522
579
static VALUE
523
580
strio_close (VALUE self )
@@ -529,10 +586,13 @@ strio_close(VALUE self)
529
586
530
587
/*
531
588
* call-seq:
532
- * strio. close_read -> nil
589
+ * close_read -> nil
533
590
*
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.
536
596
*/
537
597
static VALUE
538
598
strio_close_read (VALUE self )
@@ -547,10 +607,13 @@ strio_close_read(VALUE self)
547
607
548
608
/*
549
609
* 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.
551
615
*
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.
554
617
*/
555
618
static VALUE
556
619
strio_close_write (VALUE self )
@@ -565,9 +628,10 @@ strio_close_write(VALUE self)
565
628
566
629
/*
567
630
* call-seq:
568
- * strio. closed? -> true or false
631
+ * closed? -> true or false
569
632
*
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.
571
635
*/
572
636
static VALUE
573
637
strio_closed (VALUE self )
@@ -579,9 +643,9 @@ strio_closed(VALUE self)
579
643
580
644
/*
581
645
* call-seq:
582
- * strio. closed_read? -> true or false
646
+ * closed_read? -> true or false
583
647
*
584
- * Returns +true+ if the stream is not readable , +false+ otherwise.
648
+ * Returns +true+ if +self+ is closed for reading , +false+ otherwise.
585
649
*/
586
650
static VALUE
587
651
strio_closed_read (VALUE self )
@@ -593,9 +657,9 @@ strio_closed_read(VALUE self)
593
657
594
658
/*
595
659
* call-seq:
596
- * strio. closed_write? -> true or false
660
+ * closed_write? -> true or false
597
661
*
598
- * Returns +true+ if the stream is not writable , +false+ otherwise.
662
+ * Returns +true+ if +self+ is closed for writing , +false+ otherwise.
599
663
*/
600
664
static VALUE
601
665
strio_closed_write (VALUE self )
@@ -615,11 +679,14 @@ strio_to_read(VALUE self)
615
679
616
680
/*
617
681
* call-seq:
618
- * strio.eof -> true or false
619
- * strio.eof? -> true or false
682
+ * eof? -> true or false
620
683
*
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?.
623
690
*/
624
691
static VALUE
625
692
strio_eof (VALUE self )
@@ -1751,24 +1818,16 @@ strio_set_encoding_by_bom(VALUE self)
1751
1818
}
1752
1819
1753
1820
/*
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].
1755
1824
*
1756
- * Commonly used to simulate <code>$stdio</code> or <code>$stderr</code>
1825
+ * === About the Examples
1757
1826
*
1758
- * === Examples
1827
+ * Examples on this page assume that \StringIO has been required:
1759
1828
*
1760
1829
* require 'stringio'
1761
1830
*
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"
1772
1831
*/
1773
1832
void
1774
1833
Init_stringio (void )
0 commit comments