module IO::Like_1_8_7

This module provides most of the basic input and output functions of IO objects as implemented in Ruby version 1.8.7. Its use is supported on all versions of Ruby. See the general documentation of IO::Like for a description of how to create a class capable of using this module.

Include this module explicitly rather than IO::Like if the including class should always behave like Ruby 1.8.7 IO no matter what version of Ruby is running the class.

Constants

MBCTYPE_ASCII
MBCTYPE_EUC
MBCTYPE_SJIS
MBCTYPE_UTF8

Public Instance Methods

binmode → ios click to toggle source

Returns self. Just for compatibility with IO.

# File lib/io/like-1.8.7.rb, line 21
def binmode
  raise IOError, 'closed stream' if closed?
  self
end
bytes → anEnumerator click to toggle source

Calls each_byte without a block and returns the resulting Enumerable::Enumerator instance.

# File lib/io/like-1.8.7.rb, line 31
def bytes
  each_byte
end
chars() click to toggle source

chars is just an alias for eachchar in Ruby 1.8.7.

Alias for: each_char
each(sep_string = $/, &b) click to toggle source
Alias for: each_line
each_byte { |byte| block } → ios click to toggle source
each_byte → anEnumerator

Reads each byte (0..255) from the stream using getbyte and calls the given block once for each byte, passing the byte as an argument.

NOTE: This method ignores Errno::EAGAIN and Errno::EINTR raised by unbuffered_read. Therefore, this method always blocks. Aside from that exception and the conversion of EOFError results into nil results, this method will also raise the same errors and block at the same times as unbuffered_read.

# File lib/io/like-1.8.7.rb, line 48
def each_byte(&b)
  block_given? ?
    super(&b) :
    Enumerable::Enumerator.new(self, :each_byte)
end
each_char { |char| block } → ios click to toggle source
each_char → anEnumerator

Reads each character from the stream and calls the given block once for each character, passing the character as an argument. The character is a single or multi-byte string depending on the character to be read and the setting of $KCODE.

When called without a block, returns an instance of Enumerable::Enumerator which will iterate over each character in the same manner.

NOTE: This method ignores Errno::EAGAIN and Errno::EINTR raised by unbuffered_read. Therefore, this method always blocks. Aside from that exception and the conversion of EOFError results into nil results, this method will also raise the same errors and block at the same times as unbuffered_read.

# File lib/io/like-1.8.7.rb, line 73
def each_char
  unless block_given? then
    return Enumerable::Enumerator.new(self, :each_char)
  end

  while (byte = getbyte) do
    char = byte.chr
    # The first byte of the character was already read, so read 1 less than
    # the total number of bytes for the character to get the rest.
    __io_like__char_len(byte).downto(2) do
      byte = getbyte
      break if byte.nil?
      char << byte.chr
    end
    yield(char)
  end
  self
end
Also aliased as: chars
each_line(sep_string = $/) { |line| block } → ios click to toggle source
each_line(sep_string = $/) → anEnumerator
each(sep_string = $/) { |line| block } → ios
each(sep_string = $/) → anEnumerator

Reads each line from the stream using gets and calls the given block once for each line, passing the line as an argument.

When called without a block, returns an instance of Enumerable::Enumerator which will iterate over each line in the same manner.

NOTE: When sep_string is not nil, this method ignores Errno::EAGAIN and Errno::EINTR raised by unbuffered_read. Therefore, this method always blocks. Aside from that exception and the conversion of EOFError results into nil results, this method will also raise the same errors and block at the same times as unbuffered_read.

# File lib/io/like-1.8.7.rb, line 114
def each_line(sep_string = $/, &b)
  block_given? ?
    super(sep_string, &b) :
    Enumerable::Enumerator.new(self, :each_line, sep_string)
end
Also aliased as: each
lines(sep_string = $/) → anEnumerator click to toggle source

Calls each_line without a block and returns the resulting Enumerable::Enumerator instance.

# File lib/io/like-1.8.7.rb, line 129
def lines(sep_string = $/)
  each_line(sep_string)
end