Class: StringIO
- Inherits:
-
IO
show all
- Defined in:
- opal/stdlib/stringio.rb
Constant Summary
collapse
- VERSION =
"0"
Instance Attribute Summary collapse
Attributes inherited from IO
#lineno
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from IO
binread, #initialize_before_node_io, read, write
Constructor Details
#initialize(string = "", mode = 'rw') ⇒ StringIO
Returns a new instance of StringIO.
14
15
16
17
18
19
|
# File 'opal/stdlib/stringio.rb', line 14
def initialize(string = "", mode = 'rw')
@string = string
@position = 0
super(nil, mode)
end
|
Instance Attribute Details
Returns the value of attribute string.
12
13
14
|
# File 'opal/stdlib/stringio.rb', line 12
def string
@string
end
|
Class Method Details
.open(string = "", mode = nil, &block) ⇒ Object
4
5
6
7
8
9
10
|
# File 'opal/stdlib/stringio.rb', line 4
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
21
22
23
24
25
|
# File 'opal/stdlib/stringio.rb', line 21
def eof?
check_readable
@position == @string.length
end
|
#read(length = nil, outbuf = nil) ⇒ Object
Also known as:
readpartial
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'opal/stdlib/stringio.rb', line 83
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
|
59
60
61
|
# File 'opal/stdlib/stringio.rb', line 59
def rewind
seek 0
end
|
#seek(pos, whence = IO::SEEK_SET) ⇒ Object
Also known as:
pos=
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
52
53
|
# File 'opal/stdlib/stringio.rb', line 27
def seek(pos, whence = IO::SEEK_SET)
@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
106
107
108
109
110
|
# File 'opal/stdlib/stringio.rb', line 106
def sysread(length)
check_readable
read(length)
end
|
#tell ⇒ Object
Also known as:
pos
55
56
57
|
# File 'opal/stdlib/stringio.rb', line 55
def tell
@position
end
|
#write(string) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'opal/stdlib/stringio.rb', line 63
def write(string)
check_writable
@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
|