|
| 1 | +Reads and returns a line from the stream; |
| 2 | +returns +nil+ if at end-of-stream. |
| 3 | + |
| 4 | +Side effects: |
| 5 | + |
| 6 | +- Increments stream position by the number of bytes read. |
| 7 | +- Assigns the return value to global variable <tt>$_</tt>. |
| 8 | + |
| 9 | +With no arguments given, reads a line using the default record separator |
| 10 | +(global variable <tt>$/</tt>,* whose initial value is <tt>"\n"</tt>): |
| 11 | + |
| 12 | + strio = StringIO.new(TEXT) |
| 13 | + strio.pos # => 0 |
| 14 | + strio.gets # => "First line\n" |
| 15 | + strio.pos # => 11 |
| 16 | + $_ # => "First line\n" |
| 17 | + strio.gets # => "Second line\n" |
| 18 | + strio.read # => "\nFourth line\nFifth line\n" |
| 19 | + strio.eof? # => true |
| 20 | + strio.gets # => nil |
| 21 | + |
| 22 | + strio = StringIO.new('тест') # Four 2-byte characters. |
| 23 | + strio.pos # => 0 |
| 24 | + strio.gets # => "тест" |
| 25 | + strio.pos # => 8 |
| 26 | + |
| 27 | +<b>Argument +sep+</b> |
| 28 | + |
| 29 | +With only string argument +sep+ given, reads a line using that string as the record separator: |
| 30 | + |
| 31 | + strio = StringIO.new(TEXT) |
| 32 | + strio.gets(' ') # => "First " |
| 33 | + strio.gets(' ') # => "line\nSecond " |
| 34 | + strio.gets(' ') # => "line\n\nFourth " |
| 35 | + |
| 36 | +<b>Argument +limit+</b> |
| 37 | + |
| 38 | +With only integer argument +limit+ given, |
| 39 | +reads a line using the default record separator; |
| 40 | +limits the size (in characters) of each line to the given limit: |
| 41 | + |
| 42 | + strio = StringIO.new(TEXT) |
| 43 | + strio.gets(10) # => "First line" |
| 44 | + strio.gets(10) # => "\n" |
| 45 | + strio.gets(10) # => "Second lin" |
| 46 | + strio.gets(10) # => "e\n" |
| 47 | + |
| 48 | +<b>Arguments +sep+ and +limit+</b> |
| 49 | + |
| 50 | +With arguments +sep+ and +limit+ both given, honors both: |
| 51 | + |
| 52 | + strio = StringIO.new(TEXT) |
| 53 | + strio.gets(' ', 10) # => "First " |
| 54 | + strio.gets(' ', 10) # => "line\nSecon" |
| 55 | + strio.gets(' ', 10) # => "d " |
| 56 | + |
| 57 | +<b>Position</b> |
| 58 | + |
| 59 | +As stated above, method +gets+ reads and returns the next line in the stream. |
| 60 | + |
| 61 | +In the examples above each +strio+ object starts with its position at beginning-of-stream; |
| 62 | +but in other cases the position may be anywhere: |
| 63 | + |
| 64 | + strio = StringIO.new(TEXT) |
| 65 | + strio.pos = 12 |
| 66 | + strio.gets # => "econd line\n" |
| 67 | + |
| 68 | +The position need not be at a character boundary: |
| 69 | + |
| 70 | + strio = StringIO.new('тест') # Four 2-byte characters. |
| 71 | + strio.pos = 2 # At beginning of second character. |
| 72 | + strio.gets # => "ест" |
| 73 | + strio.pos = 3 # In middle of second character. |
| 74 | + strio.gets # => "\xB5ст" |
| 75 | + |
| 76 | +<b>Special Record Separators</b> |
| 77 | + |
| 78 | +Like some methods in class IO, method +gets+ honors two special record separators; |
| 79 | +see {Special Line Separators}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Special+Line+Separator+Values]: |
| 80 | + |
| 81 | + strio = StringIO.new(TEXT) |
| 82 | + strio.gets('') # Read "paragraph" (up to empty line). |
| 83 | + # => "First line\nSecond line\n\n" |
| 84 | + |
| 85 | + strio = StringIO.new(TEXT) |
| 86 | + strio.gets(nil) # "Slurp": read all. |
| 87 | + # => "First line\nSecond line\n\nFourth line\nFifth line\n" |
| 88 | + |
| 89 | +<b>Keyword Argument +chomp+</b> |
| 90 | + |
| 91 | +With keyword argument +chomp+ given as +true+ (the default is +false+), |
| 92 | +removes the trailing newline (if any) from the returned line: |
| 93 | + |
| 94 | + strio = StringIO.new(TEXT) |
| 95 | + strio.gets # => "First line\n" |
| 96 | + strio.gets(chomp: true) # => "Second line" |
| 97 | + |
| 98 | +Related: StringIO.each_line. |
0 commit comments