def gets(separator_or_limit = $/, limit = nil)
      fail IOError, "closed stream" if closed?

      if separator_or_limit.is_a?(Integer)
        separator = $/
        limit     = separator_or_limit
      else
        separator = separator_or_limit
      end

      return read(limit) if separator.nil?

      separator = "\n\n" if separator.empty?

      begin
        data = readpartial(limit)

        until data.include?(separator) || data.bytesize == limit || eof?
          remaining_length = limit - data.bytesize if limit
          data << readpartial(remaining_length, outbuf ||= String.new)
        end

        line, extra = data.split(separator, 2)
        line << separator if data.include?(separator)

        if cache
          cache.pos -= extra.to_s.bytesize
        else
          @buffer = @buffer.to_s.prepend(extra.to_s)
        end
      rescue EOFError
        line = nil
      end

      line
    end
def gets(separator_or_limit = $/, limit = nil)
      fail IOError, "closed stream" if closed?

      if separator_or_limit.is_a?(Integer)
        separator = $/
        limit     = separator_or_limit
      else
        separator = separator_or_limit
      end

      return read(limit) if separator.nil?

      separator = "\n\n" if separator.empty?

      begin
        data = readpartial(limit)

        until data.include?(separator) || data.bytesize == limit || eof?
          remaining_length = limit - data.bytesize if limit
          data << readpartial(remaining_length, outbuf ||= String.new)
        end

        line, extra = data.split(separator, 2)
        line << separator if data.include?(separator)

        if cache
          cache.pos -= extra.to_s.bytesize
        else
          @buffer = @buffer.to_s.prepend(extra.to_s)
        end
      rescue EOFError
        line = nil
      end

      line
    end
Implements IO#gets semantics. Without arguments it retrieves lines of
 content separated by newlines.

 With `separator` argument it does the following:

 * if `separator` is a nonempty string returns chunks of content
   surrounded with that sequence of bytes
 * if `separator` is an empty string returns paragraphs of content
   (content delimited by two newlines)
 * if `separator` is nil and `limit` is nil returns all content

 With `limit` argument returns maximum of that amount of bytes.

 Returns nil if end of file is reached. Raises IOError if closed.