Class: StringIO
- Inherits:
-
Object
- Object
- StringIO
- Includes:
- Enumerable
- Defined in:
- lib/rubysl/stringio/stringio.rb
Defined Under Namespace
Classes: Data
Constant Summary collapse
- DEFAULT_RECORD_SEPARATOR =
"\n"- Undefined =
This is why we need undefined in Ruby
Object.new
Instance Attribute Summary collapse
-
#__data__ ⇒ Object
readonly
Returns the value of attribute __data__.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #binmode ⇒ Object
- #close ⇒ Object
- #close_read ⇒ Object
- #close_write ⇒ Object
- #closed? ⇒ Boolean
- #closed_read? ⇒ Boolean
- #closed_write? ⇒ Boolean
- #each(sep = $/) ⇒ Object (also: #each_line, #lines)
- #each_byte ⇒ Object (also: #bytes)
- #each_char ⇒ Object (also: #chars)
- #eof? ⇒ Boolean (also: #eof)
- #fcntl ⇒ Object
- #fileno ⇒ Object
- #flush ⇒ Object
- #fsync ⇒ Object
- #getbyte ⇒ Object
- #getc ⇒ Object
- #gets(sep = $/) ⇒ Object
-
#initialize(string = "", mode = nil) ⇒ StringIO
constructor
A new instance of StringIO.
- #initialize_copy(from) ⇒ Object
- #isatty ⇒ Object (also: #tty?)
- #lineno ⇒ Object
- #lineno=(line) ⇒ Object
- #path ⇒ Object
- #pid ⇒ Object
- #pos ⇒ Object
- #pos=(pos) ⇒ Object
- #print(*args) ⇒ Object
- #printf(*args) ⇒ Object
- #putc(obj) ⇒ Object
- #puts(*args) ⇒ Object
- #read(length = nil, buffer = "") ⇒ Object
- #readchar ⇒ Object (also: #readbyte)
- #readline(sep = $/) ⇒ Object
- #readlines(sep = $/) ⇒ Object
- #reopen(string = nil, mode = Undefined) ⇒ Object
- #rewind ⇒ Object
- #seek(to, whence = IO::SEEK_SET) ⇒ Object
- #string ⇒ Object
- #string=(string) ⇒ Object
- #sync ⇒ Object
- #sync=(val) ⇒ Object
- #sysread(length = nil, buffer = "") ⇒ Object
- #tell ⇒ Object
- #to_yaml_properties ⇒ Object
- #truncate(length) ⇒ Object
- #ungetc(char) ⇒ Object
- #write(str) ⇒ Object (also: #syswrite)
- #yaml_initialize(type, val) ⇒ Object
Constructor Details
#initialize(string = "", mode = nil) ⇒ StringIO
Returns a new instance of StringIO.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rubysl/stringio/stringio.rb', line 34 def initialize(string="", mode=nil) string = Rubinius::Type.coerce_to string, String, :to_str @__data__ = Data.new string if mode if mode.is_a?(Integer) mode_from_integer(mode) else mode = StringValue(mode) mode_from_string(mode) end else mode_from_string(string.frozen? ? "r" : "r+") end self end |
Instance Attribute Details
#__data__ ⇒ Object (readonly)
Returns the value of attribute __data__.
32 33 34 |
# File 'lib/rubysl/stringio/stringio.rb', line 32 def __data__ @__data__ end |
Class Method Details
.open(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubysl/stringio/stringio.rb', line 19 def self.open(*args) io = new(*args) return io unless block_given? begin yield io ensure io.close io.__data__.string = nil self end end |
Instance Method Details
#<<(str) ⇒ Object
139 140 141 142 |
# File 'lib/rubysl/stringio/stringio.rb', line 139 def <<(str) write(str) self end |
#binmode ⇒ Object
144 145 146 |
# File 'lib/rubysl/stringio/stringio.rb', line 144 def binmode self end |
#close ⇒ Object
176 177 178 179 |
# File 'lib/rubysl/stringio/stringio.rb', line 176 def close raise IOError, "closed stream" if closed? @readable = @writable = nil end |
#close_read ⇒ Object
185 186 187 188 |
# File 'lib/rubysl/stringio/stringio.rb', line 185 def close_read check_readable @readable = nil end |
#close_write ⇒ Object
194 195 196 197 |
# File 'lib/rubysl/stringio/stringio.rb', line 194 def close_write check_writable @writable = nil end |
#closed? ⇒ Boolean
181 182 183 |
# File 'lib/rubysl/stringio/stringio.rb', line 181 def closed? !@readable && !@writable end |
#closed_read? ⇒ Boolean
190 191 192 |
# File 'lib/rubysl/stringio/stringio.rb', line 190 def closed_read? !@readable end |
#closed_write? ⇒ Boolean
199 200 201 |
# File 'lib/rubysl/stringio/stringio.rb', line 199 def closed_write? !@writable end |
#each(sep = $/) ⇒ Object Also known as: each_line, lines
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubysl/stringio/stringio.rb', line 125 def each(sep = $/) return to_enum :each, sep unless block_given? check_readable while line = getline(sep) yield line end self end |
#each_byte ⇒ Object Also known as: bytes
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubysl/stringio/stringio.rb', line 78 def each_byte return to_enum :each_byte unless block_given? check_readable d = @__data__ string = d.string while d.pos < string.length byte = string.getbyte d.pos d.pos += 1 yield byte end self end |
#each_char ⇒ Object Also known as: chars
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 |
# File 'lib/rubysl/stringio/stringio.rb', line 96 def each_char return to_enum :each_char unless block_given? if $KCODE == "UTF8" lookup = 7.downto(4) while c = read(1) do n = c[0] leftmost_zero_bit = lookup.find{|i| n[i].zero? } case leftmost_zero_bit when 7 # ASCII yield c when 6 # UTF 8 complementary characters next # Encoding error, ignore else more = read(6-leftmost_zero_bit) break unless more yield c+more end end else while s = read(1) yield s end end self end |
#eof? ⇒ Boolean Also known as: eof
203 204 205 206 |
# File 'lib/rubysl/stringio/stringio.rb', line 203 def eof? d = @__data__ d.pos >= d.string.size end |
#fcntl ⇒ Object
209 210 211 |
# File 'lib/rubysl/stringio/stringio.rb', line 209 def fcntl raise NotImplementedError, "StringIO#fcntl is not implemented" end |
#fileno ⇒ Object
213 214 215 |
# File 'lib/rubysl/stringio/stringio.rb', line 213 def fileno nil end |
#flush ⇒ Object
217 218 219 |
# File 'lib/rubysl/stringio/stringio.rb', line 217 def flush self end |
#fsync ⇒ Object
221 222 223 |
# File 'lib/rubysl/stringio/stringio.rb', line 221 def fsync 0 end |
#getbyte ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rubysl/stringio/stringio.rb', line 234 def getbyte check_readable d = @__data__ return nil if eof? byte = d.string.getbyte(d.pos) d.pos += 1 byte end |
#getc ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/rubysl/stringio/stringio.rb', line 225 def getc check_readable d = @__data__ char = d.string[d.pos] d.pos += 1 unless eof? char end |
#gets(sep = $/) ⇒ Object
245 246 247 248 249 |
# File 'lib/rubysl/stringio/stringio.rb', line 245 def gets(sep = $/) check_readable $_ = getline(sep) end |
#initialize_copy(from) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubysl/stringio/stringio.rb', line 52 def initialize_copy(from) from = Rubinius::Type.coerce_to(from, StringIO, :to_strio) taint if from.tainted? @append = from.instance_variable_get(:@append) @readable = from.instance_variable_get(:@readable) @writable = from.instance_variable_get(:@writable) @__data__ = from.instance_variable_get(:@__data__) self end |
#isatty ⇒ Object Also known as: tty?
251 252 253 |
# File 'lib/rubysl/stringio/stringio.rb', line 251 def isatty false end |
#lineno ⇒ Object
261 262 263 |
# File 'lib/rubysl/stringio/stringio.rb', line 261 def lineno @__data__.lineno end |
#lineno=(line) ⇒ Object
265 266 267 |
# File 'lib/rubysl/stringio/stringio.rb', line 265 def lineno=(line) @__data__.lineno = line end |
#path ⇒ Object
269 270 271 |
# File 'lib/rubysl/stringio/stringio.rb', line 269 def path nil end |
#pid ⇒ Object
273 274 275 |
# File 'lib/rubysl/stringio/stringio.rb', line 273 def pid nil end |
#pos ⇒ Object
277 278 279 |
# File 'lib/rubysl/stringio/stringio.rb', line 277 def pos @__data__.pos end |
#pos=(pos) ⇒ Object
281 282 283 284 |
# File 'lib/rubysl/stringio/stringio.rb', line 281 def pos=(pos) raise Errno::EINVAL if pos < 0 @__data__.pos = pos end |
#print(*args) ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/rubysl/stringio/stringio.rb', line 286 def print(*args) check_writable args << $_ if args.empty? args.map! { |x| x.nil? ? "nil" : x } write((args << $\).flatten.join) nil end |
#printf(*args) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/rubysl/stringio/stringio.rb', line 294 def printf(*args) check_writable if args.size > 1 write(args.shift % args) else write(args.first) end nil end |
#putc(obj) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/rubysl/stringio/stringio.rb', line 306 def putc(obj) check_writable if obj.is_a?(String) char = obj[0] else char = Rubinius::Type.coerce_to obj, Integer, :to_int end d = @__data__ pos = d.pos string = d.string if @append || pos == string.length string << char d.pos = string.length elsif pos > string.length string[string.length..pos] = "\000" * (pos - string.length) string << char d.pos = string.length else string[pos] = char d.pos += 1 end obj end |
#puts(*args) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rubysl/stringio/stringio.rb', line 334 def puts(*args) if args.empty? write(DEFAULT_RECORD_SEPARATOR) else args.each do |arg| if arg.nil? line = "nil" elsif Thread.guarding? arg line = "[...]" else begin arg = Rubinius::Type.coerce_to(arg, Array, :to_ary) Thread.recursion_guard arg do arg.each { |a| puts a } end next rescue line = arg.to_s end end write(line) write(DEFAULT_RECORD_SEPARATOR) unless line[-1] == ?\n end end nil end |
#read(length = nil, buffer = "") ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/rubysl/stringio/stringio.rb', line 363 def read(length = nil, buffer = "") check_readable d = @__data__ pos = d.pos string = d.string buffer = StringValue(buffer) if length return nil if eof? length = Rubinius::Type.coerce_to length, Integer, :to_int raise ArgumentError if length < 0 buffer.replace(string[pos, length]) d.pos += buffer.length else return "" if eof? buffer.replace(string[pos..-1]) d.pos = string.size end return buffer end |
#readchar ⇒ Object Also known as: readbyte
386 387 388 389 |
# File 'lib/rubysl/stringio/stringio.rb', line 386 def readchar raise IO::EOFError, "end of file reached" if eof? getc end |
#readline(sep = $/) ⇒ Object
393 394 395 396 397 398 |
# File 'lib/rubysl/stringio/stringio.rb', line 393 def readline(sep=$/) raise IO::EOFError, "end of file reached" if eof? check_readable $_ = getline(sep) end |
#readlines(sep = $/) ⇒ Object
400 401 402 403 404 405 406 407 408 409 |
# File 'lib/rubysl/stringio/stringio.rb', line 400 def readlines(sep=$/) check_readable ary = [] while line = getline(sep) ary << line end ary end |
#reopen(string = nil, mode = Undefined) ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/rubysl/stringio/stringio.rb', line 411 def reopen(string=nil, mode=Undefined) if string and not string.kind_of? String and mode.equal? Undefined stringio = Rubinius::Type.coerce_to(string, StringIO, :to_strio) taint if stringio.tainted? initialize_copy stringio else mode = nil if mode.equal? Undefined string = "" unless string initialize string, mode end self end |
#rewind ⇒ Object
427 428 429 430 |
# File 'lib/rubysl/stringio/stringio.rb', line 427 def rewind d = @__data__ d.pos = d.lineno = 0 end |
#seek(to, whence = IO::SEEK_SET) ⇒ Object
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/rubysl/stringio/stringio.rb', line 432 def seek(to, whence = IO::SEEK_SET) raise IOError, "closed stream" if self.closed? to = Rubinius::Type.coerce_to to, Integer, :to_int case whence when IO::SEEK_CUR to += @__data__.pos when IO::SEEK_END to += @__data__.string.size when IO::SEEK_SET, nil else raise Errno::EINVAL, "invalid whence" end raise Errno::EINVAL if to < 0 @__data__.pos = to return 0 end |
#string ⇒ Object
458 459 460 |
# File 'lib/rubysl/stringio/stringio.rb', line 458 def string @__data__.string end |
#string=(string) ⇒ Object
462 463 464 465 466 467 |
# File 'lib/rubysl/stringio/stringio.rb', line 462 def string=(string) d = @__data__ d.string = StringValue(string) d.pos = 0 d.lineno = 0 end |
#sync ⇒ Object
469 470 471 |
# File 'lib/rubysl/stringio/stringio.rb', line 469 def sync true end |
#sync=(val) ⇒ Object
473 474 475 |
# File 'lib/rubysl/stringio/stringio.rb', line 473 def sync=(val) val end |
#sysread(length = nil, buffer = "") ⇒ Object
477 478 479 480 481 |
# File 'lib/rubysl/stringio/stringio.rb', line 477 def sysread(length=nil, buffer="") str = read(length, buffer) raise IO::EOFError, "end of file reached" if str.nil? str end |
#tell ⇒ Object
483 484 485 |
# File 'lib/rubysl/stringio/stringio.rb', line 483 def tell @__data__.pos end |
#to_yaml_properties ⇒ Object
522 523 524 |
# File 'lib/rubysl/stringio/stringio.rb', line 522 def to_yaml_properties [] end |
#truncate(length) ⇒ Object
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/rubysl/stringio/stringio.rb', line 487 def truncate(length) check_writable len = Rubinius::Type.coerce_to length, Integer, :to_int raise Errno::EINVAL, "negative length" if len < 0 string = @__data__.string if len < string.size string[len..string.size] = "" else string << "\000" * (len - string.size) end return length end |
#ungetc(char) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/rubysl/stringio/stringio.rb', line 501 def ungetc(char) check_readable d = @__data__ pos = d.pos string = d.string char = Rubinius::Type.coerce_to char, Integer, :to_int if pos > string.size string[string.size..pos] = "\000" * (pos - string.size) d.pos -= 1 string[d.pos] = char elsif pos > 0 d.pos -= 1 string[d.pos] = char end nil end |
#write(str) ⇒ Object Also known as: syswrite
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 'lib/rubysl/stringio/stringio.rb', line 148 def write(str) check_writable str = String(str) return 0 if str.empty? d = @__data__ pos = d.pos string = d.string if @append || pos == string.length string << str d.pos = string.length elsif pos > string.size string[string.size..pos] = "\000" * (pos - string.size) string << str d.pos = string.size else string[pos, str.length] = str d.pos += str.length string.taint if str.tainted? end return str.length end |