Class: IO

Inherits:
Object show all
Defined in:
opal/opal/corelib/io.rb

Direct Known Subclasses

File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd, flags = 'r') ⇒ IO

Returns a new instance of IO.



15
16
17
18
19
20
21
22
23
24
25
# File 'opal/opal/corelib/io.rb', line 15

def initialize(fd, flags = 'r')
  @fd = fd
  @flags = flags
  @eof = false

  if flags.include?('r') && !flags.match?(/[wa+]/)
    @closed = :write
  elsif flags.match?(/[wa]/) && !flags.match?(/[r+]/)
    @closed = :read
  end
end

Instance Attribute Details

#eofObject (readonly) Also known as: eof?

Returns the value of attribute eof.



11
12
13
# File 'opal/opal/corelib/io.rb', line 11

def eof
  @eof
end

#read_procObject

Returns the value of attribute read_proc.



13
14
15
# File 'opal/opal/corelib/io.rb', line 13

def read_proc
  @read_proc
end

#syncObject

Returns the value of attribute sync.



13
14
15
# File 'opal/opal/corelib/io.rb', line 13

def sync
  @sync
end

#ttyObject

Returns the value of attribute tty.



13
14
15
# File 'opal/opal/corelib/io.rb', line 13

def tty
  @tty
end

#write_procObject

Returns the value of attribute write_proc.



13
14
15
# File 'opal/opal/corelib/io.rb', line 13

def write_proc
  @write_proc
end

Instance Method Details

#<<(string) ⇒ Object



44
45
46
47
# File 'opal/opal/corelib/io.rb', line 44

def <<(string)
  write(string)
  self
end

#check_readableObject



293
294
295
296
297
# File 'opal/opal/corelib/io.rb', line 293

def check_readable
  if closed_read?
    ::Kernel.raise ::IOError, 'not opened for reading'
  end
end

#check_writableObject



286
287
288
289
290
# File 'opal/opal/corelib/io.rb', line 286

def check_writable
  if closed_write?
    ::Kernel.raise ::IOError, 'not opened for writing'
  end
end

#closeObject

Closedness



253
254
255
# File 'opal/opal/corelib/io.rb', line 253

def close
  @closed = :both
end

#close_readObject



257
258
259
260
261
262
263
# File 'opal/opal/corelib/io.rb', line 257

def close_read
  if @closed == :write
    @closed = :both
  else
    @closed = :read
  end
end

#close_writeObject



265
266
267
268
269
270
271
# File 'opal/opal/corelib/io.rb', line 265

def close_write
  if @closed == :read
    @closed = :both
  else
    @closed = :write
  end
end

#closed?Boolean

Returns:



273
274
275
# File 'opal/opal/corelib/io.rb', line 273

def closed?
  @closed == :both
end

#closed_read?Boolean

Returns:



277
278
279
# File 'opal/opal/corelib/io.rb', line 277

def closed_read?
  @closed == :read || @closed == :both
end

#closed_write?Boolean

Returns:



281
282
283
# File 'opal/opal/corelib/io.rb', line 281

def closed_write?
  @closed == :write || @closed == :both
end

#each(sep = $/, *args, &block) ⇒ Object Also known as: each_line



221
222
223
224
225
226
227
228
229
# File 'opal/opal/corelib/io.rb', line 221

def each(sep = $/, *args, &block)
  return enum_for :each, sep, *args unless block_given?

  while (s = gets(sep, *args))
    yield(s)
  end

  self
end

#each_byte(&block) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'opal/opal/corelib/io.rb', line 231

def each_byte(&block)
  return enum_for :each_byte unless block_given?

  while (s = getbyte)
    yield(s)
  end

  self
end

#each_char(&block) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'opal/opal/corelib/io.rb', line 241

def each_char(&block)
  return enum_for :each_char unless block_given?

  while (s = getc)
    yield(s)
  end

  self
end

#filenoObject



27
28
29
# File 'opal/opal/corelib/io.rb', line 27

def fileno
  @fd
end

#flushObject



40
41
42
# File 'opal/opal/corelib/io.rb', line 40

def flush
  # noop
end

#getbyteObject



105
106
107
# File 'opal/opal/corelib/io.rb', line 105

def getbyte
  getc&.ord
end

#getcObject

Reading



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'opal/opal/corelib/io.rb', line 87

def getc
  @read_buffer ||= ''
  parts = ''

  # Will execure at most twice - one time reading from a buffer
  # second time executing read proc
  begin
    @read_buffer += parts
    if @read_buffer != ''
      ret = @read_buffer[0]
      @read_buffer = @read_buffer[1..-1]
      return ret
    end
  end while parts = sysread_noraise(1)

  nil
end

#gets(sep = false, limit = nil, opts = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
# File 'opal/opal/corelib/io.rb', line 121

def gets(sep = false, limit = nil, opts = {})
  if `sep.$$is_number` && !limit
    sep, limit, opts = false, sep, limit
  end
  if `sep.$$is_hash` && !limit && opts == {}
    sep, limit, opts = false, nil, sep
  elsif `limit.$$is_hash` && opts == {}
    sep, limit, opts = sep, nil, limit
  end

  orig_sep = sep

  sep = $/ if sep == false
  sep = /\r?\n\r?\n/ if sep == ''
  sep ||= ''
  sep = sep.to_str unless orig_sep == ''

  # Try to deduce length of a regexp
  seplen = orig_sep == '' ? 2 : sep.length

  sep = / / if sep == ' ' # WTF is this, String#split(" ") matches all whitespaces???

  @read_buffer ||= ''
  data = ''
  ret = nil

  begin
    @read_buffer += data
    if sep != '' && (`sep.$$is_regexp` ? @read_buffer.match?(sep) : @read_buffer.include?(sep))
      orig_buffer = @read_buffer
      ret, @read_buffer = @read_buffer.split(sep, 2)
      ret += orig_buffer[ret.length, seplen] if ret != orig_buffer
      break
    end
  end while data = sysread_noraise(sep == '' ? 65_536 : 1)

  unless ret
    ret, @read_buffer = (@read_buffer || ''), ''
    ret = nil if ret == ''
  end

  if ret
    if limit
      ret = ret[0...limit]
      @read_buffer = ret[limit..-1] + @read_buffer
    end
    ret = ret.sub(/\r?\n\z/, '') if opts[:chomp]
    ret = ret.sub(/\A[\r\n]+/, '') if orig_sep == ''
  end

  $_ = ret if orig_sep == false
  ret
end


49
50
51
52
53
54
55
56
57
# File 'opal/opal/corelib/io.rb', line 49

def print(*args)
  %x{
    for (var i = 0, ii = args.length; i < ii; i++) {
      args[i] = #{::Kernel.String(`args[i]`)}
    }
    self.$write(args.join(#{$,}));
  }
  nil
end

#puts(*args) ⇒ Object



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
# File 'opal/opal/corelib/io.rb', line 59

def puts(*args)
  %x{
    var line
    if (args.length === 0) {
      #{write "\n"};
      return nil;
    } else {
      for (var i = 0, ii = args.length; i < ii; i++) {
        if (args[i].$$is_array){
          var ary = #{`args[i]`.flatten}
          if (ary.length > 0) #{puts(*`ary`)}
        } else {
          if (args[i].$$is_string) {
            line = args[i].valueOf();
          } else {
            line = #{::Kernel.String(`args[i]`)};
          }
          if (!line.endsWith("\n")) line += "\n"
          #{write `line`}
        }
      }
    }
  }
  nil
end

#read(integer = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'opal/opal/corelib/io.rb', line 198

def read(integer = nil)
  @read_buffer ||= ''
  parts = ''
  ret = nil

  begin
    @read_buffer += parts
    if integer && @read_buffer.length > integer
      ret, @read_buffer = @read_buffer[0...integer], @read_buffer[integer..-1]
      return ret
    end
  end while parts = sysread_noraise(integer || 65_536)

  ret, @read_buffer = @read_buffer, ''
  ret
end

#readbyteObject



109
110
111
# File 'opal/opal/corelib/io.rb', line 109

def readbyte
  readchar.ord
end

#readcharObject



113
114
115
# File 'opal/opal/corelib/io.rb', line 113

def readchar
  getc || ::Kernel.raise(::EOFError, 'end of file reached')
end

#readline(*args) ⇒ Object



117
118
119
# File 'opal/opal/corelib/io.rb', line 117

def readline(*args)
  gets(*args) || ::Kernel.raise(::EOFError, 'end of file reached')
end

#readlines(separator = $/) ⇒ Object

Eaches



217
218
219
# File 'opal/opal/corelib/io.rb', line 217

def readlines(separator = $/)
  each_line(separator).to_a
end

#readpartial(integer) ⇒ Object



190
191
192
193
194
195
196
# File 'opal/opal/corelib/io.rb', line 190

def readpartial(integer)
  @read_buffer ||= ''
  part = sysread(integer)
  ret, @read_buffer = @read_buffer + (part || ''), ''
  ret = nil if ret == ''
  ret
end

#sysread(integer) ⇒ Object

This method is to be overloaded, or read_proc can be changed



176
177
178
179
180
181
# File 'opal/opal/corelib/io.rb', line 176

def sysread(integer)
  `self.read_proc(integer)` || begin
    @eof = true
    ::Kernel.raise ::EOFError, 'end of file reached'
  end
end

#sysread_noraise(integer) ⇒ Object



184
185
186
187
188
# File 'opal/opal/corelib/io.rb', line 184

def sysread_noraise(integer)
  sysread(integer)
rescue ::EOFError
  nil
end

#tty?Boolean

Returns:



31
32
33
# File 'opal/opal/corelib/io.rb', line 31

def tty?
  `self.tty == true`
end

#write(string) ⇒ Object



35
36
37
38
# File 'opal/opal/corelib/io.rb', line 35

def write(string)
  `self.write_proc(string)`
  string.size
end