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
22 def binmode
23   raise IOError, 'closed stream' if closed?
24   self
25 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
32 def bytes
33   each_byte
34 end
chars()

chars is just an alias for eachchar in Ruby 1.8.7.

Alias for: each_char
each(sep_string = $/) { |line| block } → ios
each(sep_string = $/) → anEnumerator
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.

Calls superclass method IO::Like_1_8_6#each_byte
   # File lib/io/like-1.8.7.rb
49 def each_byte(&b)
50   block_given? ?
51     super(&b) :
52     Enumerable::Enumerator.new(self, :each_byte)
53 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
74 def each_char
75   unless block_given? then
76     return Enumerable::Enumerator.new(self, :each_char)
77   end
78 
79   while (byte = getbyte) do
80     char = byte.chr
81     # The first byte of the character was already read, so read 1 less than
82     # the total number of bytes for the character to get the rest.
83     __io_like__char_len(byte).downto(2) do
84       byte = getbyte
85       break if byte.nil?
86       char << byte.chr
87     end
88     yield(char)
89   end
90   self
91 end
Also aliased as: chars
each_line(sep_string = $/) -> anEnumerator click to toggle source

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.

Calls superclass method IO::Like_1_8_6#each_line
    # File lib/io/like-1.8.7.rb
115 def each_line(sep_string = $/, &b)
116   block_given? ?
117     super(sep_string, &b) :
118     Enumerable::Enumerator.new(self, :each_line, sep_string)
119 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
130 def lines(sep_string = $/)
131   each_line(sep_string)
132 end

Private Instance Methods

__io_like__char_len(byte) click to toggle source

Given byte which represents the first byte of a possibly multi-byte character, returns the total number of bytes for the character based on the setting of $KCODE.

    # File lib/io/like-1.8.7.rb
218 def __io_like__char_len(byte)
219   # Get the first byte of $KCODE on all versions of Ruby up to 1.9.1.
220   kcode = $KCODE.respond_to?(:getbyte) ? $KCODE.getbyte(0) : $KCODE[0]
221 
222   # This is essentially what the Ruby guts do to find the right lookup
223   # table.
224   case kcode
225   when ?E, ?e
226     MBCTYPE_EUC[byte]
227   when ?S, ?s
228     MBCTYPE_SJIS[byte]
229   when ?U, ?u
230     MBCTYPE_UTF8[byte]
231   else # when ?N, ?n, ?A, ?a
232     MBCTYPE_ASCII[byte]
233   end
234 end