Class: StringIO
- Includes:
- IO::Readable, IO::Writable
- Defined in:
- opal/stdlib/stringio.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
Returns the value of attribute string.
Class Method Summary collapse
Instance Method Summary collapse
- #check_readable ⇒ Object
- #check_writable ⇒ Object
- #close ⇒ Object
- #close_read ⇒ Object
- #close_write ⇒ Object
- #closed? ⇒ Boolean
- #closed_read? ⇒ Boolean
- #closed_write? ⇒ Boolean
- #each_byte(&block) ⇒ Object
- #each_char(&block) ⇒ Object
- #eof? ⇒ Boolean (also: #eof)
-
#initialize(string = "", mode = 'rw') ⇒ StringIO
constructor
A new instance of StringIO.
- #read(length = nil, outbuf = nil) ⇒ Object
- #rewind ⇒ Object
- #seek(pos, whence = IO::SEEK_SET) ⇒ Object (also: #pos=)
- #tell ⇒ Object (also: #pos)
- #write(string) ⇒ Object
Constructor Details
#initialize(string = "", mode = 'rw') ⇒ StringIO
Returns a new instance of StringIO
15 16 17 18 19 20 21 22 23 24 |
# File 'opal/stdlib/stringio.rb', line 15 def initialize(string = "", mode = 'rw') @string = string @position = string.length if mode.include?('r') and not mode.include?('w') @closed = :write elsif mode.include?('w') and not mode.include?('r') @closed = :read end end |
Instance Attribute Details
#string ⇒ Object
Returns the value of attribute string
13 14 15 |
# File 'opal/stdlib/stringio.rb', line 13 def string @string end |
Class Method Details
.open(string = "", mode = nil, &block) ⇒ Object
5 6 7 8 9 10 11 |
# File 'opal/stdlib/stringio.rb', line 5 def self.open(string = "", mode = nil, &block) io = new(string, mode) res = block.call(io) io.close res end |
Instance Method Details
#check_readable ⇒ Object
176 177 178 179 180 |
# File 'opal/stdlib/stringio.rb', line 176 def check_readable if closed_read? raise IOError, "not opened for reading" end end |
#check_writable ⇒ Object
170 171 172 173 174 |
# File 'opal/stdlib/stringio.rb', line 170 def check_writable if closed_write? raise IOError, "not opened for writing" end end |
#close ⇒ Object
138 139 140 |
# File 'opal/stdlib/stringio.rb', line 138 def close @closed = :both end |
#close_read ⇒ Object
142 143 144 145 146 147 148 |
# File 'opal/stdlib/stringio.rb', line 142 def close_read if @closed == :write @closed = :both else @closed = :read end end |
#close_write ⇒ Object
150 151 152 153 154 155 156 |
# File 'opal/stdlib/stringio.rb', line 150 def close_write if @closed == :read @closed = :both else @closed = :write end end |
#closed? ⇒ Boolean
158 159 160 |
# File 'opal/stdlib/stringio.rb', line 158 def closed? @closed == :both end |
#closed_read? ⇒ Boolean
162 163 164 |
# File 'opal/stdlib/stringio.rb', line 162 def closed_read? @closed == :read || @closed == :both end |
#closed_write? ⇒ Boolean
166 167 168 |
# File 'opal/stdlib/stringio.rb', line 166 def closed_write? @closed == :write || @closed == :both end |
#each_byte(&block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'opal/stdlib/stringio.rb', line 71 def each_byte(&block) return enum_for :each_byte unless block check_readable i = @position until eof? block.call(@string[i].ord) i += 1 end self end |
#each_char(&block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'opal/stdlib/stringio.rb', line 85 def each_char(&block) return enum_for :each_char unless block check_readable i = @position until eof? block.call(@string[i]) i += 1 end self end |
#eof? ⇒ Boolean Also known as: eof
26 27 28 29 30 |
# File 'opal/stdlib/stringio.rb', line 26 def eof? check_readable @position == @string.length end |
#read(length = nil, outbuf = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'opal/stdlib/stringio.rb', line 116 def read(length = nil, outbuf = nil) check_readable return if eof? string = if length str = @string[@position, length] @position += length str else str = @string[@position .. -1] @position = @string.length str end if outbuf outbuf.write(string) else string end end |
#rewind ⇒ Object
67 68 69 |
# File 'opal/stdlib/stringio.rb', line 67 def rewind seek 0 end |
#seek(pos, whence = IO::SEEK_SET) ⇒ Object Also known as: pos=
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'opal/stdlib/stringio.rb', line 34 def seek(pos, whence = IO::SEEK_SET) 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 |
#tell ⇒ Object Also known as: pos
59 60 61 |
# File 'opal/stdlib/stringio.rb', line 59 def tell @position end |
#write(string) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'opal/stdlib/stringio.rb', line 99 def write(string) check_writable 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 |