Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] StringIO doc enhancements #33

Merged
merged 4 commits into from
Oct 16, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 89 additions & 42 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,18 @@ strio_s_allocate(VALUE klass)
}

/*
* call-seq: StringIO.new(string=""[, mode])
* call-seq:
* StringIO.new(string = '', mode = 'r+') -> new_stringio
*
* Returns a new \StringIO instance formed from +string+ and +mode+;
* see {Access Modes}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Access+Modes]:
*
* strio = StringIO.new # => #<StringIO>
* strio.close
*
* Creates new StringIO instance from with _string_ and _mode_.
* The instance should be closed when no longer needed.
*
* Related: StringIO.open (accepts block; closes automatically).
*/
static VALUE
strio_initialize(int argc, VALUE *argv, VALUE self)
Expand Down Expand Up @@ -392,11 +401,24 @@ strio_finalize(VALUE self)
}

/*
* call-seq: StringIO.open(string=""[, mode]) {|strio| ...}
* call-seq:
* StringIO.open(string = '', mode = 'r+') {|strio| ... }
*
* Creates a new \StringIO instance formed from +string+ and +mode+;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mode is defaulted to r if string is frozen.
Is this worth to describe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to ::new and ::open..

* see {Access Modes}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Access+Modes].
*
* Equivalent to StringIO.new except that when it is called with a block, it
* yields with the new instance and closes it, and returns the result which
* returned from the block.
* With no block, returns the new instance:
*
* strio = StringIO.open # => #<StringIO>
*
* With a block, calls the block with the new instance
* and returns the block's value;
* closes the instance on block exit.
*
* StringIO.open {|strio| p strio }
* # => #<StringIO>
*
* Related: StringIO.new.
*/
static VALUE
strio_s_open(int argc, VALUE *argv, VALUE klass)
Expand Down Expand Up @@ -482,9 +504,23 @@ strio_unimpl(int argc, VALUE *argv, VALUE self)
}

/*
* call-seq: strio.string -> string
* call-seq:
* string -> string
*
* Returns underlying string:
*
* Returns underlying String object, the subject of IO.
* StringIO.open('foo') do |strio|
* p strio.string
* strio.string = 'bar'
* p strio.string
* end
*
* Output:
*
* "foo"
* "bar"
*
* Related: StringIO#string= (assigns the underlying string).
*/
static VALUE
strio_get_string(VALUE self)
Expand All @@ -494,9 +530,23 @@ strio_get_string(VALUE self)

/*
* call-seq:
* strio.string = string -> string
* string = string -> string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this confusing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

*
* Assigns the underlying string as +string+;
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
* returns +string+:
*
* StringIO.open('foo') do |strio|
* p strio.string
* strio.string = 'bar'
* p strio.string
* end
*
* Output:
*
* Changes underlying String object, the subject of IO.
* "foo"
* "bar"
*
* Related: StringIO#string (returns the underlying string).
*/
static VALUE
strio_set_string(VALUE self, VALUE string)
Expand All @@ -514,10 +564,11 @@ strio_set_string(VALUE self, VALUE string)

/*
* call-seq:
* strio.close -> nil
* close -> nil
*
* Closes +self+ for both reading and writing.
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
*
* Closes a StringIO. The stream is unavailable for any further data
* operations; an +IOError+ is raised if such an attempt is made.
* Related: StringIO#close_read, StringIO#close#write.
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
*/
static VALUE
strio_close(VALUE self)
Expand All @@ -529,10 +580,11 @@ strio_close(VALUE self)

/*
* call-seq:
* strio.close_read -> nil
* close_read -> nil
*
* Closes +self+ for reading; closed-write setting remains unchanged.
*
* Closes the read end of a StringIO. Will raise an +IOError+ if the
* receiver is not readable.
* Related: StringIO#clase, StringIO#close_write.
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
*/
static VALUE
strio_close_read(VALUE self)
Expand All @@ -547,10 +599,11 @@ strio_close_read(VALUE self)

/*
* call-seq:
* strio.close_write -> nil
* close_write -> nil
*
* Closes the write end of a StringIO. Will raise an +IOError+ if the
* receiver is not writeable.
* Closes +self+ for writing; closed-read setting remains unchanged.
*
* Related: StringIO#clase, StringIO#close_read.
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
*/
static VALUE
strio_close_write(VALUE self)
Expand All @@ -565,9 +618,10 @@ strio_close_write(VALUE self)

/*
* call-seq:
* strio.closed? -> true or false
* closed? -> true or false
*
* Returns +true+ if the stream is completely closed, +false+ otherwise.
* Returns +true+ if +self+ is closed for both reading and writing,
* +false+ otherwise.
*/
static VALUE
strio_closed(VALUE self)
Expand All @@ -579,9 +633,9 @@ strio_closed(VALUE self)

/*
* call-seq:
* strio.closed_read? -> true or false
* closed_read? -> true or false
*
* Returns +true+ if the stream is not readable, +false+ otherwise.
* Returns +true+ if +self+ is closed for reading, +false+ otherwise.
*/
static VALUE
strio_closed_read(VALUE self)
Expand All @@ -593,9 +647,9 @@ strio_closed_read(VALUE self)

/*
* call-seq:
* strio.closed_write? -> true or false
* closed_write? -> true or false
*
* Returns +true+ if the stream is not writable, +false+ otherwise.
* Returns +true+ if +self+ is closed for writing, +false+ otherwise.
*/
static VALUE
strio_closed_write(VALUE self)
Expand All @@ -615,11 +669,12 @@ strio_to_read(VALUE self)

/*
* call-seq:
* strio.eof -> true or false
* strio.eof? -> true or false
* eof? -> true or false
*
* Returns +true+ if positioned at end-of-stream +false+ otherwise;
peterzhu2118 marked this conversation as resolved.
Show resolved Hide resolved
* see {Position}[https://docs.ruby-lang.org/en/master/File.html#class-File-label-Position].
*
* Returns true if the stream is at the end of the data (underlying string).
* The stream must be opened for reading or an +IOError+ will be raised.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this sentence needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added (three places).

* StreamIO#eof is an alias for StreamIO#eof?.
*/
static VALUE
strio_eof(VALUE self)
Expand Down Expand Up @@ -1751,24 +1806,16 @@ strio_set_encoding_by_bom(VALUE self)
}

/*
* Pseudo I/O on String object, with interface corresponding to IO.
* \IO streams for strings, with access similar to
* {IO}[https://docs.ruby-lang.org/en/master/IO.html];
* see {IO Streams}[https://docs.ruby-lang.org/en/master/io_streams_rdoc.html].
*
* Commonly used to simulate <code>$stdio</code> or <code>$stderr</code>
* === About the Examples
*
* === Examples
* Examples on this page assume that \StringIO has been required:
*
* require 'stringio'
*
* # Writing stream emulation
* io = StringIO.new
* io.puts "Hello World"
* io.string #=> "Hello World\n"
*
* # Reading stream emulation
* io = StringIO.new "first\nsecond\nlast\n"
* io.getc #=> "f"
* io.gets #=> "irst\n"
* io.read #=> "second\nlast\n"
*/
void
Init_stringio(void)
Expand Down