Module: FakeIO
- Includes:
- Enumerable, File::Constants
- Defined in:
- lib/fake_io.rb,
lib/fake_io/version.rb
Overview
The FakeIO module provides an API for communicating with controlled resources, that is still compatible with the standard IO class.
To utilize the FakeIO module, simply include it into a class and define either #io_read and/or #io_write, to handle the reading and writing of data.
The #io_open method handles optionally opening and assigning the file descriptor for the IO stream. The #io_close method handles optionally closing the IO stream.
Constant Summary collapse
- VERSION =
fake_io version
'0.1.0'
Instance Attribute Summary collapse
-
#autoclose ⇒ Object
writeonly
Sets whether the IO stream will be auto-closed when finalized.
-
#close_on_exec ⇒ Object
writeonly
Sets the close-on-exec flag.
-
#eof ⇒ Object
(also: #eof?)
readonly
The end-of-file indicator.
-
#external_encoding ⇒ Encoding
The external encoding to convert all read data into.
-
#internal_encoding ⇒ Encoding?
The internal encoding to convert all internal data to.
-
#pid ⇒ Integer?
readonly
The PID associated with the IO stream.
-
#pos ⇒ Object
(also: #tell)
The position within the IO stream.
-
#sync ⇒ Boolean
The sync flag.
Abstract Methods collapse
-
#io_close ⇒ Object
protected
abstract
Place holder method used to close the IO stream.
-
#io_open ⇒ fd
protected
abstract
Place holder method used to open the IO stream.
-
#io_read ⇒ String
protected
abstract
Place holder method used to read a block from the IO stream.
-
#io_seek(new_pos, whence) ⇒ Object
protected
abstract
Place holder method used to seek to a position within the IO stream.
-
#io_write(data) ⇒ Object
protected
abstract
Place holder method used to write data to the IO stream.
Instance Method Summary collapse
-
#advise(advice, offset = 0, len = 0) ⇒ nil
Announce an intention to access data from the current file in a specific pattern.
- #autoclose? ⇒ true
- #binmode ⇒ IO
- #binmode? ⇒ Boolean
-
#bytes ⇒ Object
deprecated
Deprecated.
Removed in Ruby 3.0.
-
#chars ⇒ Object
deprecated
Deprecated.
Removed in Ruby 3.0.
-
#close ⇒ Object
Closes the IO stream.
-
#close_on_exec? ⇒ Boolean
Indicates whether the close-on-exec flag is set.
-
#close_read ⇒ Object
Closes the read end of a duplex IO stream.
-
#close_write ⇒ Object
Closes the write end of a duplex IO stream.
-
#closed? ⇒ Boolean
Determines whether the IO stream is closed.
-
#codepoints ⇒ Object
deprecated
Deprecated.
Removed in Ruby 3.0
-
#each_byte {|byte| ... } ⇒ Enumerator
Iterates over each byte in the IO stream.
-
#each_char {|char| ... } ⇒ Enumerator
Iterates over each character in the IO stream.
-
#each_chunk {|block| ... } ⇒ Enumerator
Iterates over each block within the IO stream.
-
#each_codepoint {|ord| ... } ⇒ Enumerator
Passes the Integer ordinal of each character in the stream.
-
#each_line(separator = $/) {|line| ... } ⇒ Enumerator
(also: #each)
Iterates over each line in the IO stream.
- #fcntl(command, argument) ⇒ Object
-
#fileno ⇒ Integer
The file descriptor.
- #flush ⇒ IO
-
#fsync ⇒ 0
(also: #fdatasync)
Immediately writes all buffered data.
-
#getbyte ⇒ Integer
Reads a byte from the IO stream.
-
#getc ⇒ String
Reads a character from the IO stream.
-
#gets(separator = $/) ⇒ String
Reads a string from the IO stream.
-
#initialize ⇒ Object
Initializes the IO stream.
-
#inspect ⇒ String
Inspects the IO stream.
- #ioctl(command, argument) ⇒ Object
-
#isatty ⇒ Boolean
Indicates whether the IO stream is associated with a terminal device.
-
#lineno ⇒ Integer
The current line-number (how many times #gets has been called).
-
#lineno=(number) ⇒ Integer
Manually sets the current line-number.
-
#lines(*args) ⇒ Object
deprecated
Deprecated.
Removed in Ruby 3.0.
-
#open ⇒ IO
protected
Opens the IO stream.
-
#pread(maxlen, offset, outbuf) ⇒ String
Reads data at a given offset, without changing the current #pos.
-
#print(*arguments) ⇒ nil
Prints data to the IO stream.
-
#printf(format_string, *arguments) ⇒ nil
Prints a formatted string to the IO stream.
-
#putc(data) ⇒ String, Integer
Writes a byte or a character to the IO stream.
-
#puts(*arguments) ⇒ nil
Prints data with new-line characters to the IO stream.
-
#pwrite(string, offset) ⇒ Integer
Writes data to the given offset, without changing the current #pos.
-
#read(length = nil, buffer = nil) ⇒ String
(also: #sysread, #read_nonblock)
Reads data from the IO stream.
-
#readbyte ⇒ Integer
Reads a byte from the IO stream.
-
#readchar ⇒ Integer
Reads a character from the IO stream.
-
#readline(separator = $/) ⇒ String
Reads a line from the IO stream.
-
#readlines(separator = $/) ⇒ Array<String>
Reads every line from the IO stream.
-
#readpartial(length, buffer = nil) ⇒ String
Reads partial data from the IO stream.
- #reopen(*arguments) ⇒ Object
- #rewind ⇒ IO
- #seek(new_pos, whence = SEEK_SET) ⇒ 0
-
#set_encoding(*arguments) ⇒ Object
Sets the encoding.
-
#set_encoding_by_bom ⇒ Object
Sets the #external_encoding based on the Byte Order Mark (BOM) bytes at the beginning of the file.
- #stat ⇒ Object
- #sysseek(offset, whence = SEEK_SET) ⇒ Object
-
#to_i ⇒ Integer
The file descriptor.
- #to_io ⇒ IO
- #tty? ⇒ Boolean
-
#ungetbyte(byte) ⇒ nil
Un-reads a byte from the IO stream, append it to the read buffer.
-
#ungetc(char) ⇒ nil
Un-reads a character from the IO stream, append it to the read buffer.
-
#write(data) ⇒ Integer
(also: #syswrite, #write_nonblock, #<<)
Writes data to the IO stream.
Instance Attribute Details
#autoclose=(value) ⇒ Object (writeonly)
For compatibility with IO.
Sets whether the IO stream will be auto-closed when finalized.
894 895 896 |
# File 'lib/fake_io.rb', line 894 def autoclose=(value) @autoclose = value end |
#close_on_exec=(value) ⇒ Object (writeonly)
For compatibility with IO.
Sets the close-on-exec flag.
908 909 910 |
# File 'lib/fake_io.rb', line 908 def close_on_exec=(value) @close_on_exec = value end |
#eof ⇒ Object (readonly) Also known as: eof?
The end-of-file indicator
25 26 27 |
# File 'lib/fake_io.rb', line 25 def eof @eof end |
#external_encoding ⇒ Encoding
The external encoding to convert all read data into.
32 33 34 |
# File 'lib/fake_io.rb', line 32 def external_encoding @external_encoding end |
#internal_encoding ⇒ Encoding?
The internal encoding to convert all internal data to.
37 38 39 |
# File 'lib/fake_io.rb', line 37 def internal_encoding @internal_encoding end |
#pid ⇒ Integer? (readonly)
The PID associated with the IO stream.
764 765 766 |
# File 'lib/fake_io.rb', line 764 def pid @pid end |
#pos ⇒ Object Also known as: tell
The position within the IO stream
20 21 22 |
# File 'lib/fake_io.rb', line 20 def pos @pos end |
#sync ⇒ Boolean
The sync flag.
956 957 958 |
# File 'lib/fake_io.rb', line 956 def sync @sync end |
Instance Method Details
#advise(advice, offset = 0, len = 0) ⇒ nil
Not implemented by default.
Announce an intention to access data from the current file in a specific pattern.
80 81 82 |
# File 'lib/fake_io.rb', line 80 def advise(advice,offset=0,len=0) # no-op end |
#autoclose? ⇒ true
For compatibility with IO.
901 902 903 |
# File 'lib/fake_io.rb', line 901 def autoclose? @autoclose end |
#binmode ⇒ IO
For compatibility with IO.
877 878 879 880 |
# File 'lib/fake_io.rb', line 877 def binmode @binmode = true return self end |
#binmode? ⇒ Boolean
For compatibility with IO.
887 888 889 |
# File 'lib/fake_io.rb', line 887 def binmode? @binmode == true end |
#bytes ⇒ Object
Removed in Ruby 3.0.
Deprecated alias to #each_bytes.
502 503 504 |
# File 'lib/fake_io.rb', line 502 def bytes each_byte end |
#chars ⇒ Object
Removed in Ruby 3.0.
Deprecated alias to #each_char.
531 532 533 |
# File 'lib/fake_io.rb', line 531 def chars each_char end |
#close ⇒ Object
Closes the IO stream.
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |
# File 'lib/fake_io.rb', line 1010 def close io_close @fd = nil @read = false @write = false @closed = true return nil end |
#close_on_exec? ⇒ Boolean
For compatibility with IO.
Indicates whether the close-on-exec flag is set.
917 918 919 |
# File 'lib/fake_io.rb', line 917 def close_on_exec? @close_on_exec end |
#close_read ⇒ Object
Closes the read end of a duplex IO stream.
978 979 980 981 982 983 984 |
# File 'lib/fake_io.rb', line 978 def close_read if @write then @read = false else close end return nil end |
#close_write ⇒ Object
Closes the write end of a duplex IO stream.
989 990 991 992 993 994 995 |
# File 'lib/fake_io.rb', line 989 def close_write if @read then @write = false else close end return nil end |
#closed? ⇒ Boolean
Determines whether the IO stream is closed.
1003 1004 1005 |
# File 'lib/fake_io.rb', line 1003 def closed? @closed == true end |
#codepoints ⇒ Object
Removed in Ruby 3.0
Deprecated alias to #each_codepoint.
563 564 565 |
# File 'lib/fake_io.rb', line 563 def codepoints each_codepoint end |
#each_byte {|byte| ... } ⇒ Enumerator
Iterates over each byte in the IO stream.
490 491 492 493 494 |
# File 'lib/fake_io.rb', line 490 def each_byte(&block) return enum_for(__method__) unless block each_chunk { |chunk| chunk.each_byte(&block) } end |
#each_char {|char| ... } ⇒ Enumerator
Iterates over each character in the IO stream.
519 520 521 522 523 |
# File 'lib/fake_io.rb', line 519 def each_char(&block) return enum_for(__method__) unless block each_chunk { |chunk| chunk.each_char(&block) } end |
#each_chunk {|block| ... } ⇒ Enumerator
Iterates over each block within the IO stream.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/fake_io.rb', line 216 def each_chunk return enum_for(__method__) unless block_given? unless @read raise(IOError,"closed for reading") end unless io_buffer_empty? # read from the buffer first chunk = io_buffer_read chunk.force_encoding(external_encoding) yield chunk end until @eof begin # no data currently available, sleep and retry until (chunk = io_read) sleep(1) end rescue EOFError @eof = true break end chunk.force_encoding(internal_encoding) if internal_encoding chunk.force_encoding(external_encoding) unless chunk.empty? yield chunk else # short read @eof = true end end end |
#each_codepoint {|ord| ... } ⇒ Enumerator
Only available on Ruby > 1.9.
Passes the Integer ordinal of each character in the stream.
551 552 553 554 555 |
# File 'lib/fake_io.rb', line 551 def each_codepoint return enum_for(__method__) unless block_given? each_char { |c| yield c.ord } end |
#each_line(separator = $/) {|line| ... } ⇒ Enumerator Also known as: each
Iterates over each line in the IO stream.
582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/fake_io.rb', line 582 def each_line(separator=$/) return enum_for(__method__,separator) unless block_given? loop do begin line = gets(separator) rescue EOFError break end yield line end end |
#fcntl(command, argument) ⇒ Object
933 934 935 |
# File 'lib/fake_io.rb', line 933 def fcntl(command,argument) raise(NotImplementedError,"#{self.class}#fcntl was not implemented") end |
#fileno ⇒ Integer
For compatibility with IO.
The file descriptor
1028 1029 1030 |
# File 'lib/fake_io.rb', line 1028 def fileno @fd end |
#flush ⇒ IO
For compatibility with IO.
963 964 965 |
# File 'lib/fake_io.rb', line 963 def flush self end |
#fsync ⇒ 0 Also known as: fdatasync
For compatibility with IO.
Immediately writes all buffered data.
944 945 946 947 |
# File 'lib/fake_io.rb', line 944 def fsync flush return 0 end |
#getbyte ⇒ Integer
Only available on Ruby > 1.9.
Reads a byte from the IO stream.
329 330 331 332 333 |
# File 'lib/fake_io.rb', line 329 def getbyte if (c = read(1)) c.bytes.first end end |
#getc ⇒ String
Reads a character from the IO stream.
341 342 343 |
# File 'lib/fake_io.rb', line 341 def getc read(1) end |
#gets(separator = $/) ⇒ String
Reads a string from the IO stream.
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/fake_io.rb', line 396 def gets(separator=$/) # increment the line number @lineno += 1 # if no separator is given, read everything return read if separator.nil? line = String.new(encoding: external_encoding) while (c = read(1)) line << c break if c == separator # separator reached end if line.empty? # a line should atleast contain the separator raise(EOFError,"end of file reached") end return line end |
#initialize ⇒ Object
Initializes the IO stream.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fake_io.rb', line 42 def initialize @read = true @write = true @closed = true @autoclose = true @close_on_exec = true @binmode = false @tty = false @external_encoding = Encoding.default_external @internal_encoding = Encoding.default_internal @sync = false open end |
#inspect ⇒ String
Inspects the IO stream.
1058 1059 1060 |
# File 'lib/fake_io.rb', line 1058 def inspect "#<#{self.class}: #{@fd.inspect if @fd}>" end |
#io_close ⇒ Object (protected)
Place holder method used to close the IO stream.
1145 1146 |
# File 'lib/fake_io.rb', line 1145 def io_close end |
#io_open ⇒ fd (protected)
Place holder method used to open the IO stream.
1094 1095 |
# File 'lib/fake_io.rb', line 1094 def io_open end |
#io_read ⇒ String (protected)
Place holder method used to read a block from the IO stream.
1125 1126 |
# File 'lib/fake_io.rb', line 1125 def io_read end |
#io_seek(new_pos, whence) ⇒ Object (protected)
Place holder method used to seek to a position within the IO stream.
1110 1111 1112 |
# File 'lib/fake_io.rb', line 1110 def io_seek(new_pos,whence) raise(NotImplementedError,"#{self.class}#io_seek is not implemented") end |
#io_write(data) ⇒ Object (protected)
Place holder method used to write data to the IO stream.
1136 1137 1138 |
# File 'lib/fake_io.rb', line 1136 def io_write(data) 0 end |
#ioctl(command, argument) ⇒ Object
925 926 927 |
# File 'lib/fake_io.rb', line 925 def ioctl(command,argument) raise(NotImplementedError,"#{self.class}#ioctl was not implemented") end |
#isatty ⇒ Boolean
For compatibility with IO.
Indicates whether the IO stream is associated with a terminal device.
781 782 783 |
# File 'lib/fake_io.rb', line 781 def isatty @tty end |
#lineno ⇒ Integer
The current line-number (how many times #gets has been called).
830 831 832 833 834 835 836 |
# File 'lib/fake_io.rb', line 830 def lineno unless @read raise(IOError,"not opened for reading") end return @lineno end |
#lineno=(number) ⇒ Integer
Manually sets the current line-number.
850 851 852 853 854 855 856 |
# File 'lib/fake_io.rb', line 850 def lineno=(number) unless @read raise(IOError,"not opened for reading") end return @lineno = number.to_i end |
#lines(*args) ⇒ Object
Removed in Ruby 3.0.
Deprecated alias to #each_line.
604 605 606 |
# File 'lib/fake_io.rb', line 604 def lines(*args) each_line(*args) end |
#open ⇒ IO (protected)
Opens the IO stream.
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
# File 'lib/fake_io.rb', line 1070 def open @pos = 0 @lineno = 0 @eof = false io_buffer_clear! @fd = io_open @closed = false return self end |
#pread(maxlen, offset, outbuf) ⇒ String
Reads data at a given offset, without changing the current #pos.
665 666 667 668 669 670 671 672 |
# File 'lib/fake_io.rb', line 665 def pread(maxlen,offset,outbuf) old_pos = pos seek(offset) data = read(maxlen,outbuf) seek(old_pos) return data end |
#print(*arguments) ⇒ nil
Prints data to the IO stream.
724 725 726 727 |
# File 'lib/fake_io.rb', line 724 def print(*arguments) arguments.each { |data| write(data) } return nil end |
#printf(format_string, *arguments) ⇒ nil
Prints a formatted string to the IO stream.
753 754 755 756 |
# File 'lib/fake_io.rb', line 753 def printf(format_string,*arguments) write(format_string % arguments) return nil end |
#putc(data) ⇒ String, Integer
Writes a byte or a character to the IO stream.
706 707 708 709 710 711 712 713 714 |
# File 'lib/fake_io.rb', line 706 def putc(data) char = case data when String then data.chr else data end write(char) return data end |
#puts(*arguments) ⇒ nil
Prints data with new-line characters to the IO stream.
737 738 739 740 |
# File 'lib/fake_io.rb', line 737 def puts(*arguments) arguments.each { |data| write("#{data}#{$/}") } return nil end |
#pwrite(string, offset) ⇒ Integer
Writes data to the given offset, without changing the current #pos.
688 689 690 691 692 693 694 695 |
# File 'lib/fake_io.rb', line 688 def pwrite(string,offset) old_pos = pos seek(offset) bytes_written = write(string) seek(old_pos) return bytes_written end |
#read(length = nil, buffer = nil) ⇒ String Also known as: sysread, read_nonblock
Reads data from the IO stream.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/fake_io.rb', line 267 def read(length=nil,buffer=nil) bytes_remaining = (length || Float::INFINITY) result = String.new(encoding: external_encoding) each_chunk do |chunk| if bytes_remaining < chunk.bytesize fragment = chunk.byteslice(0,bytes_remaining) remaining_length = chunk.bytesize - bytes_remaining remaining_data = chunk.byteslice(bytes_remaining,remaining_length) result << fragment io_buffer_append(remaining_data) @pos += bytes_remaining break else result << chunk bytes_read = chunk.bytesize bytes_remaining -= bytes_read @pos += bytes_read end # no more data to read break if bytes_remaining == 0 end unless result.empty? buffer << result if buffer return result end end |
#readbyte ⇒ Integer
Reads a byte from the IO stream.
447 448 449 450 451 452 453 |
# File 'lib/fake_io.rb', line 447 def readbyte unless (c = read(1)) raise(EOFError,"end of file reached") end return c.bytes.first end |
#readchar ⇒ Integer
Reads a character from the IO stream.
430 431 432 433 434 435 436 |
# File 'lib/fake_io.rb', line 430 def readchar unless (c = getc) raise(EOFError,"end of file reached") end return c end |
#readline(separator = $/) ⇒ String
Reads a line from the IO stream.
470 471 472 473 474 475 476 |
# File 'lib/fake_io.rb', line 470 def readline(separator=$/) unless (line = gets(separator)) raise(EOFError,"end of file reached") end return line end |
#readlines(separator = $/) ⇒ Array<String>
Reads every line from the IO stream.
617 618 619 |
# File 'lib/fake_io.rb', line 617 def readlines(separator=$/) enum_for(:each_line,separator).to_a end |
#readpartial(length, buffer = nil) ⇒ String
Reads partial data from the IO stream.
316 317 318 |
# File 'lib/fake_io.rb', line 316 def readpartial(length,buffer=nil) read(length,buffer) end |
#reopen(*arguments) ⇒ Object
971 972 973 |
# File 'lib/fake_io.rb', line 971 def reopen(*arguments) raise(NotImplementedError,"#{self.class}#reopen is not implemented") end |
#rewind ⇒ IO
For compatibility with IO.
865 866 867 868 869 870 |
# File 'lib/fake_io.rb', line 865 def rewind seek(0,SEEK_SET) @pos = 0 @lineno = 0 end |
#seek(new_pos, whence = SEEK_SET) ⇒ 0
800 801 802 803 804 |
# File 'lib/fake_io.rb', line 800 def seek(new_pos,whence=SEEK_SET) io_seek(new_pos,whence) io_buffer_clear! return 0 end |
#set_encoding(encoding) ⇒ Object #set_encoding(ext_inc, int_enc) ⇒ Object
Sets the encoding.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fake_io.rb', line 107 def set_encoding(*arguments) if arguments.length == 1 case arguments[0] when String string = arguments[0] if string.include?(':') || string.include?(',') ext_enc, int_enc = string.split(/[:,]\s*/,2) self.external_encoding = Encoding.find(ext_enc) self.internal_encoding = Encoding.find(int_enc) else self.external_encoding = Encoding.find(string) end when Encoding self.external_encoding = arguments[0] else raise(TypeError,"argument must be a String or Encoding object") end elsif arguments.length == 2 || arguments.length == 3 self.external_encoding = arguments[0] self.internal_encoding = arguments[1] else raise(ArgumentError,"wrong number of arguments (given #{arguments.length}, expected 1..3)") end end |
#set_encoding_by_bom ⇒ Object
Sets the #external_encoding based on the Byte Order Mark (BOM) bytes at the beginning of the file.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/fake_io.rb', line 138 def set_encoding_by_bom case (b1 = getbyte) when 0x00 # possible UTF-32 (BE) b2 = getbyte b3 = getbyte b4 = getbyte if (b2 == 0x00) && (b3 == 0xFE) && (b4 == 0xFF) # UTF-32 (BE) BOM (0x00 0x00 0xFE 0xFF) detected self.external_encoding = Encoding::UTF_32BE else ungetbyte(b4) if b4 ungetbyte(b3) if b3 ungetbyte(b2) if b2 ungetbyte(b1) if b1 end when 0x28 # possible UTF-7 b2 = getbyte b3 = getbyte if (b2 == 0x2F) && (b3 == 0x76) # UTF-7 BOM (0x28 0x2F 0x76) detected self.external_encoding = Encoding::UTF_7 else ungetbyte(b3) if b3 ungetbyte(b2) if b2 ungetbyte(b1) if b1 end when 0xEF # possible UTF-8 b2 = getbyte b3 = getbyte if (b2 == 0xBB) && (b3 == 0xBF) self.external_encoding = Encoding::UTF_8 else ungetbyte(b3) if b3 ungetbyte(b2) if b2 ungetbyte(b1) if b1 end when 0xFE # possible UTF-16 (BE) b2 = getbyte if (b2 == 0xFF) self.external_encoding = Encoding::UTF_16BE else ungetbyte(b2) if b2 ungetbyte(b1) if b1 end when 0xFF # possible UTF-16 (LE) or UTF-32 (LE) b2 = getbyte if (b2 == 0xFE) self.external_encoding = Encoding::UTF_16LE else ungetbyte(b2) if b2 ungetbyte(b1) if b1 end else ungetbyte(b1) if b1 end end |
#stat ⇒ Object
770 771 772 |
# File 'lib/fake_io.rb', line 770 def stat raise(NotImplementedError,"#{self.class}#stat is not implemented") end |
#sysseek(offset, whence = SEEK_SET) ⇒ Object
809 810 811 |
# File 'lib/fake_io.rb', line 809 def sysseek(offset,whence=SEEK_SET) seek(new_pos,whence) end |
#to_i ⇒ Integer
For compatibility with IO.
The file descriptor
1039 1040 1041 |
# File 'lib/fake_io.rb', line 1039 def to_i @fd end |
#to_io ⇒ IO
For compatibility with IO.
1048 1049 1050 |
# File 'lib/fake_io.rb', line 1048 def to_io self end |
#tty? ⇒ Boolean
788 789 790 |
# File 'lib/fake_io.rb', line 788 def tty? @tty end |
#ungetbyte(byte) ⇒ nil
Only available on Ruby > 1.9.
Un-reads a byte from the IO stream, append it to the read buffer.
357 358 359 360 361 362 363 364 365 366 |
# File 'lib/fake_io.rb', line 357 def ungetbyte(byte) char = case byte when Integer then byte.chr else byte.to_s end io_buffer_prepend(char) @pos -= char.bytesize return nil end |
#ungetc(char) ⇒ nil
Un-reads a character from the IO stream, append it to the read buffer.
378 379 380 381 382 383 384 |
# File 'lib/fake_io.rb', line 378 def ungetc(char) char = char.to_s io_buffer_prepend(char) @pos -= char.bytesize return nil end |
#write(data) ⇒ Integer Also known as: syswrite, write_nonblock, <<
Writes data to the IO stream.
633 634 635 636 637 638 639 640 641 642 |
# File 'lib/fake_io.rb', line 633 def write(data) unless @write raise(IOError,"closed for writing") end data = data.to_s data.force_encoding(internal_encoding) if internal_encoding io_write(data) end |