Class: StringIO
Instance Attribute Summary collapse
-
#string ⇒ Object
Returns the value of attribute string.
Attributes inherited from IO
Class Method Summary collapse
Instance Method Summary collapse
- #eof? ⇒ Boolean (also: #eof)
-
#initialize(string = "", mode = 'rw') ⇒ StringIO
constructor
A new instance of StringIO.
- #read(length = nil, outbuf = nil) ⇒ Object (also: #readpartial)
- #rewind ⇒ Object
- #seek(pos, whence = IO::SEEK_SET) ⇒ Object (also: #pos=)
- #sysread(length) ⇒ Object
- #tell ⇒ Object (also: #pos)
- #write(string) ⇒ Object
Methods inherited from IO
binread , #initialize_before_node_io , read , write
Constructor Details
#initialize(string = "", mode = 'rw') ⇒ StringIO
Returns a new instance of StringIO.
12 13 14 15 16 17
# File 'opal/stdlib/stringio.rb', line 12 def initialize(string = "", mode = 'rw') @string = string @position = 0 super(nil, mode) end
Instance Attribute Details
#string ⇒ Object
Returns the value of attribute string.
10 11 12
# File 'opal/stdlib/stringio.rb', line 10 def string @string end
Class Method Details
.open(string = "", mode = nil, &block) ⇒ Object
2 3 4 5 6 7 8
# File 'opal/stdlib/stringio.rb', line 2 def self.open(string = "", mode = nil, &block) io = new(string, mode) res = block.call(io) io.close res end
Instance Method Details
#eof? ⇒ Boolean Also known as: eof
Returns:
- (Boolean )
19 20 21 22 23
# File 'opal/stdlib/stringio.rb', line 19 def eof? check_readable @position == @string.length end
#read(length = nil, outbuf = nil) ⇒ Object Also known as: readpartial
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
# File 'opal/stdlib/stringio.rb', line 81 def read(length = nil, outbuf = nil) check_readable return if eof? string = if length str = @string[@position, length] @position += length @position = @string.length if @position > @string.length str else str = @string[@position .. -1] @position = @string.length str end if outbuf outbuf.write(string) else string end end
#rewind ⇒ Object
57 58 59
# File 'opal/stdlib/stringio.rb', line 57 def rewind seek 0 end
#seek(pos, whence = IO::SEEK_SET) ⇒ Object Also known as: pos=
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# File 'opal/stdlib/stringio.rb', line 25 def seek(pos, whence = IO ::SEEK_SET) # Let's reset the read buffer, because it will be most likely wrong @read_buffer = '' case whence when IO ::SEEK_SET raise Errno::EINVAL unless pos >= 0 @position = pos when IO ::SEEK_CUR if @position + pos > @string.length @position = @string.length else @position += pos end when IO ::SEEK_END if pos > @string.length @position = 0 else @position -= pos end end 0 end
#sysread(length) ⇒ Object
104 105 106 107 108
# File 'opal/stdlib/stringio.rb', line 104 def sysread(length) check_readable read(length) end
#tell ⇒ Object Also known as: pos
53 54 55
# File 'opal/stdlib/stringio.rb', line 53 def tell @position end
#write(string) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# File 'opal/stdlib/stringio.rb', line 61 def write(string) check_writable # Let's reset the read buffer, because it will be most likely wrong @read_buffer = '' string = String (string) if @string.length == @position @string += string @position += string.length else before = @string[0 .. @position - 1] after = @string[@position + string.length .. -1] @string = before + string + after @position += string.length end end