Class: IO
Overview
backtick_javascript: true
Direct Known Subclasses
Instance Attribute Summary collapse
-
#eof ⇒ Object
(also: #eof?)
readonly
Returns the value of attribute eof.
-
#read_proc ⇒ Object
Returns the value of attribute read_proc.
-
#sync ⇒ Object
Returns the value of attribute sync.
-
#tty ⇒ Object
Returns the value of attribute tty.
-
#write_proc ⇒ Object
Returns the value of attribute write_proc.
Instance Method Summary collapse
- #<<(string) ⇒ Object
- #check_readable ⇒ Object
- #check_writable ⇒ Object
-
#close ⇒ Object
Closedness.
- #close_read ⇒ Object
- #close_write ⇒ Object
- #closed? ⇒ Boolean
- #closed_read? ⇒ Boolean
- #closed_write? ⇒ Boolean
- #each(sep = $/, *args, &block) ⇒ Object (also: #each_line)
- #each_byte(&block) ⇒ Object
- #each_char(&block) ⇒ Object
- #fileno ⇒ Object
- #flush ⇒ Object
- #getbyte ⇒ Object
-
#getc ⇒ Object
Reading.
- #gets(sep = false, limit = nil, opts = {}) ⇒ Object
-
#initialize(fd, flags = 'r') ⇒ IO
constructor
A new instance of IO.
- #print(*args) ⇒ Object
- #puts(*args) ⇒ Object
- #read(integer = nil) ⇒ Object
- #readbyte ⇒ Object
- #readchar ⇒ Object
- #readline(*args) ⇒ Object
-
#readlines(separator = $/) ⇒ Object
Eaches.
- #readpartial(integer) ⇒ Object
-
#sysread(integer) ⇒ Object
This method is to be overloaded, or read_proc can be changed.
- #sysread_noraise(integer) ⇒ Object
- #tty? ⇒ Boolean
- #write(string) ⇒ Object
Constructor Details
#initialize(fd, flags = 'r') ⇒ IO
Returns a new instance of IO.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'opal/opal/corelib/io.rb', line 17 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
#eof ⇒ Object (readonly) Also known as: eof?
Returns the value of attribute eof.
13 14 15 |
# File 'opal/opal/corelib/io.rb', line 13 def eof @eof end |
#read_proc ⇒ Object
Returns the value of attribute read_proc.
15 16 17 |
# File 'opal/opal/corelib/io.rb', line 15 def read_proc @read_proc end |
#sync ⇒ Object
Returns the value of attribute sync.
15 16 17 |
# File 'opal/opal/corelib/io.rb', line 15 def sync @sync end |
#tty ⇒ Object
Returns the value of attribute tty.
15 16 17 |
# File 'opal/opal/corelib/io.rb', line 15 def tty @tty end |
#write_proc ⇒ Object
Returns the value of attribute write_proc.
15 16 17 |
# File 'opal/opal/corelib/io.rb', line 15 def write_proc @write_proc end |
Instance Method Details
#<<(string) ⇒ Object
46 47 48 49 |
# File 'opal/opal/corelib/io.rb', line 46 def <<(string) write(string) self end |
#check_readable ⇒ Object
295 296 297 298 299 |
# File 'opal/opal/corelib/io.rb', line 295 def check_readable if closed_read? ::Kernel.raise ::IOError, 'not opened for reading' end end |
#check_writable ⇒ Object
288 289 290 291 292 |
# File 'opal/opal/corelib/io.rb', line 288 def check_writable if closed_write? ::Kernel.raise ::IOError, 'not opened for writing' end end |
#close ⇒ Object
Closedness
255 256 257 |
# File 'opal/opal/corelib/io.rb', line 255 def close @closed = :both end |
#close_read ⇒ Object
259 260 261 262 263 264 265 |
# File 'opal/opal/corelib/io.rb', line 259 def close_read if @closed == :write @closed = :both else @closed = :read end end |
#close_write ⇒ Object
267 268 269 270 271 272 273 |
# File 'opal/opal/corelib/io.rb', line 267 def close_write if @closed == :read @closed = :both else @closed = :write end end |
#closed? ⇒ Boolean
275 276 277 |
# File 'opal/opal/corelib/io.rb', line 275 def closed? @closed == :both end |
#closed_read? ⇒ Boolean
279 280 281 |
# File 'opal/opal/corelib/io.rb', line 279 def closed_read? @closed == :read || @closed == :both end |
#closed_write? ⇒ Boolean
283 284 285 |
# File 'opal/opal/corelib/io.rb', line 283 def closed_write? @closed == :write || @closed == :both end |
#each(sep = $/, *args, &block) ⇒ Object Also known as: each_line
223 224 225 226 227 228 229 230 231 |
# File 'opal/opal/corelib/io.rb', line 223 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
233 234 235 236 237 238 239 240 241 |
# File 'opal/opal/corelib/io.rb', line 233 def each_byte(&block) return enum_for :each_byte unless block_given? while (s = getbyte) yield(s) end self end |
#each_char(&block) ⇒ Object
243 244 245 246 247 248 249 250 251 |
# File 'opal/opal/corelib/io.rb', line 243 def each_char(&block) return enum_for :each_char unless block_given? while (s = getc) yield(s) end self end |
#fileno ⇒ Object
29 30 31 |
# File 'opal/opal/corelib/io.rb', line 29 def fileno @fd end |
#flush ⇒ Object
42 43 44 |
# File 'opal/opal/corelib/io.rb', line 42 def flush # noop end |
#getbyte ⇒ Object
107 108 109 |
# File 'opal/opal/corelib/io.rb', line 107 def getbyte getc&.ord end |
#getc ⇒ Object
Reading
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'opal/opal/corelib/io.rb', line 89 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
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 174 175 |
# File 'opal/opal/corelib/io.rb', line 123 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 |
#print(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'opal/opal/corelib/io.rb', line 51 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
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 |
# File 'opal/opal/corelib/io.rb', line 61 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'opal/opal/corelib/io.rb', line 200 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 |
#readbyte ⇒ Object
111 112 113 |
# File 'opal/opal/corelib/io.rb', line 111 def readbyte readchar.ord end |
#readchar ⇒ Object
115 116 117 |
# File 'opal/opal/corelib/io.rb', line 115 def readchar getc || ::Kernel.raise(::EOFError, 'end of file reached') end |
#readline(*args) ⇒ Object
119 120 121 |
# File 'opal/opal/corelib/io.rb', line 119 def readline(*args) gets(*args) || ::Kernel.raise(::EOFError, 'end of file reached') end |
#readlines(separator = $/) ⇒ Object
Eaches
219 220 221 |
# File 'opal/opal/corelib/io.rb', line 219 def readlines(separator = $/) each_line(separator).to_a end |
#readpartial(integer) ⇒ Object
192 193 194 195 196 197 198 |
# File 'opal/opal/corelib/io.rb', line 192 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
178 179 180 181 182 183 |
# File 'opal/opal/corelib/io.rb', line 178 def sysread(integer) `self.read_proc(integer)` || begin @eof = true ::Kernel.raise ::EOFError, 'end of file reached' end end |
#sysread_noraise(integer) ⇒ Object
186 187 188 189 190 |
# File 'opal/opal/corelib/io.rb', line 186 def sysread_noraise(integer) sysread(integer) rescue ::EOFError nil end |
#write(string) ⇒ Object
37 38 39 40 |
# File 'opal/opal/corelib/io.rb', line 37 def write(string) `self.write_proc(string)` string.size end |