forked from ksss/mruby-stringio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringio.rb
125 lines (106 loc) · 1.96 KB
/
stringio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class StringIO
include Enumerable
READABLE = 0x0001
WRITABLE = 0x0002
READWRITE = READABLE | WRITABLE
BINMODE = 0x0004
APPEND = 0x0040
CREATE = 0x0080
TRUNC = 0x0800
DEFAULT_RS = "\n"
class << self
def open(string = "", mode = "r+", &block)
instance = new string, mode
block.call instance if block
instance
ensure
instance.string = nil
instance.close unless instance.closed?
end
end
attr_accessor :string
alias_method :tell, :pos
def print(*strings)
strings.each do |string|
str = string.to_s
write str
end
nil
end
def puts(*strings)
strings.each do |string|
str = string.to_s
write str
if str.length == 0 || str[str.length-1] != DEFAULT_RS
write DEFAULT_RS
end
end
nil
end
def read_nonblock(*args)
option = { exception: true }
if Hash === args.last
option = args.pop
if !option.key?(:exception)
option[:exception] = true
end
end
if args.length == 0
raise ArgumentError, "wrong number of arguments (given 0, expected 1..2)"
end
str = read(*args)
if str.nil?
if option[:exception]
raise EOFError, "end of file reached"
else
nil
end
end
str
end
def write_nonblock(*args)
case args.last
when Hash, NilClass
args.pop
end
write(*args)
end
def sysread(*args)
str = read(*args)
if str == nil
raise EOFError, "end of file reached"
end
str
end
def readchar
c = getc
raise EOFError, 'end of file reached' if c.nil?
c
end
def each(*args, &block)
return to_enum :each unless block
while line = gets(*args)
block.call(line)
end
end
alias each_line each
def fileno
nil
end
def tty?
false
end
alias_method :isatty, :tty?
def sync
true
end
def sync=(val)
val
end
def fsync
0
end
def flush
self
end
end