Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- mrbgems/mruby-io/mrblib/io.rb
Direct Known Subclasses
Class Method Summary collapse
- .open(*args, &block) ⇒ Object
- .pipe(&block) ⇒ Object
- .popen(command, mode = 'r', **opts, &block) ⇒ Object
- .read(path, length = nil, offset = 0, mode: "r") ⇒ Object
Instance Method Summary collapse
- #<<(str) ⇒ Object
-
#each(&block) ⇒ Object
(also: #each_line)
15.2.20.5.3.
-
#each_byte(&block) ⇒ Object
15.2.20.5.4.
- #each_char(&block) ⇒ Object
- #hash ⇒ Object
- #pos=(i) ⇒ Object
- #print(*args) ⇒ Object
- #printf(*args) ⇒ Object
- #puts(*args) ⇒ Object
- #rewind ⇒ Object
- #ungetbyte(c) ⇒ Object
Class Method Details
.open(*args, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 10 def self.open(*args, &block) io = self.new(*args) return io unless block begin yield io ensure begin io.close unless io.closed? rescue StandardError end end end |
.pipe(&block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 43 def self.pipe(&block) if !self.respond_to?(:_pipe) raise NotImplementedError, "pipe is not supported on this platform" end if block begin r, w = IO._pipe yield r, w ensure r.close unless r.closed? w.close unless w.closed? end else IO._pipe end end |
.popen(command, mode = 'r', **opts, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 25 def self.popen(command, mode = 'r', **opts, &block) if !self.respond_to?(:_popen) raise NotImplementedError, "popen is not supported on this platform" end io = self._popen(command, mode, **opts) return io unless block begin yield io ensure begin io.close unless io.closed? rescue IOError # nothing end end end |
.read(path, length = nil, offset = 0, mode: "r") ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 60 def self.read(path, length=nil, offset=0, mode: "r") str = "" fd = -1 io = nil begin fd = IO.sysopen(path, mode) io = IO.open(fd, mode) io.seek(offset) if offset > 0 str = io.read(length) ensure if io io.close elsif fd != -1 IO._sysclose(fd) end end str end |
Instance Method Details
#<<(str) ⇒ Object
85 86 87 88 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 85 def <<(str) write(str) self end |
#each(&block) ⇒ Object Also known as: each_line
15.2.20.5.3
113 114 115 116 117 118 119 120 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 113 def each(&block) return to_enum unless block while line = self.gets block.call(line) end self end |
#each_byte(&block) ⇒ Object
15.2.20.5.4
123 124 125 126 127 128 129 130 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 123 def each_byte(&block) return to_enum(:each_byte) unless block while byte = self.getbyte block.call(byte) end self end |
#each_char(&block) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 135 def each_char(&block) return to_enum(:each_char) unless block while char = self.getc block.call(char) end self end |
#hash ⇒ Object
79 80 81 82 83 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 79 def hash # We must define IO#hash here because IO includes Enumerable and # Enumerable#hash will call IO#read() otherwise self.__id__ end |
#pos=(i) ⇒ Object
93 94 95 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 93 def pos=(i) seek(i, SEEK_SET) end |
#print(*args) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 165 def print(*args) i = 0 len = args.size while i < len write args[i].to_s i += 1 end end |
#printf(*args) ⇒ Object
174 175 176 177 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 174 def printf(*args) write sprintf(*args) nil end |
#puts(*args) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 144 def puts(*args) i = 0 len = args.size if len == 0 write "\n" return end while i < len s = args[i] if s.kind_of?(Array) puts(*s) if s.size > 0 else s = s.to_s write s write "\n" if (s[-1] != "\n") end i += 1 end nil end |
#rewind ⇒ Object
97 98 99 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 97 def rewind seek(0, SEEK_SET) end |
#ungetbyte(c) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'mrbgems/mruby-io/mrblib/io.rb', line 101 def ungetbyte(c) if c.is_a? String c = c.getbyte(0) else c &= 0xff end s = " " s.setbyte(0,c) ungetc s end |