Class: IO

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/mruby-io/mrblib/io.rb

Direct Known Subclasses

BasicSocket, File

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(*args, &block) ⇒ Object

call-seq:

IO.open(fd, mode="r" [, opt])                -> io
IO.open(fd, mode="r" [, opt]) {|io| block }  -> obj

With no associated block, IO.open is a synonym for IO.new. If the optional code block is given, it will be passed io as an argument, and the IO object will automatically be closed when the block terminates. In this instance, IO.open returns the value of the block.

fd = IO.sysopen("/dev/tty", "w")
a = IO.open(fd,"w")
$stderr.puts "Hello"
a.close


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'mrbgems/mruby-io/mrblib/io.rb', line 25

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

call-seq:

IO.pipe                    -> [read_io, write_io]
IO.pipe {|read_io, write_io| ... } -> obj

Creates a pair of pipe endpoints (connected to each other) and returns them as a two-element array of IO objects: [read_io, write_io].

rd, wr = IO.pipe
if fork
wr.close
puts rd.read
rd.close
Process.wait
else
rd.close
wr.write "Hello, parent!"
wr.close
exit
end


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'mrbgems/mruby-io/mrblib/io.rb', line 94

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

call-seq:

IO.popen(cmd, mode="r" [, opt])               -> io
IO.popen(cmd, mode="r" [, opt]) {|io| block } -> obj

Runs the specified command as a subprocess; the subprocess's standard input and output will be connected to the returned IO object.

p IO.popen("date").read   #=> "Wed Apr  9 08:56:30 CDT 2003\n"
IO.popen("dc", "r+") {|f|
f.puts "5 2 *"
f.close_write
puts f.read
}


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'mrbgems/mruby-io/mrblib/io.rb', line 55

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

call-seq:

IO.read(name, [length [, offset]] )   -> string
IO.read(name, [length [, offset]], mode: mode)   -> string

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.

IO.read("testfile")           #=> "This is line one\nThis is line two\n"
IO.read("testfile", 20)       #=> "This is line one\nTh"
IO.read("testfile", 20, 10)   #=> "ne one\nThis is line "


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'mrbgems/mruby-io/mrblib/io.rb', line 124

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

#each(&block) ⇒ Object Also known as: each_line

call-seq:

ios.each(sep=$/) {|line| block }         -> ios
ios.each(limit) {|line| block }          -> ios
ios.each(sep,limit) {|line| block }      -> ios
ios.each(...)                            -> an_enumerator

Executes the block for every line in ios, where lines are separated by sep. ios must be opened for reading. If no block is given, an enumerator is returned instead.

f = File.new("testfile")
f.each {|line| puts "#{f.lineno}: #{line}" }

15.2.20.5.3



212
213
214
215
216
217
218
219
# File 'mrbgems/mruby-io/mrblib/io.rb', line 212

def each(&block)
  return to_enum(:each) unless block

  while line = self.gets
    block.call(line)
  end
  self
end

#each_byte(&block) ⇒ Object

call-seq:

ios.each_byte {|byte| block }  -> ios
ios.each_byte                  -> an_enumerator

Calls the given block once for each byte (0..255) in ios, passing the byte as an argument. The stream must be opened for reading or an IOError will be raised.

f = File.new("testfile")
checksum = 0
f.each_byte {|x| checksum ^= x }   #=> #<File:testfile>
checksum                           #=> 12

15.2.20.5.4



235
236
237
238
239
240
241
242
# File 'mrbgems/mruby-io/mrblib/io.rb', line 235

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

call-seq:

ios.each_char {|c| block }  -> ios
ios.each_char               -> an_enumerator

Calls the given block once for each character in ios, passing the character as an argument. The stream must be opened for reading or an IOError will be raised.

f = File.new("testfile")
ios.each_char {|c| print c, ' ' }   #=> #<File:testfile>


258
259
260
261
262
263
264
265
# File 'mrbgems/mruby-io/mrblib/io.rb', line 258

def each_char(&block)
  return to_enum(:each_char) unless block

  while char = self.getc
    block.call(char)
  end
  self
end

#hashObject

call-seq:

ios.hash   -> integer

Compute a hash based on the IO object. Two IO objects with the same content will have the same hash code (and will compare using eql?). We must define IO#hash here because IO includes Enumerable and Enumerable#hash will call IO#read() otherwise.



154
155
156
157
158
# File 'mrbgems/mruby-io/mrblib/io.rb', line 154

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

call-seq:

ios.pos = integer    -> integer

Seeks to the given position (in bytes) in ios. It is not guaranteed that seeking to the right position when ios is textmode.

f = File.new("testfile")
f.pos = 17
f.gets   #=> "This is line two\n"


177
178
179
# File 'mrbgems/mruby-io/mrblib/io.rb', line 177

def pos=(i)
  seek(i, SEEK_SET)
end

#printf(*args) ⇒ Object

call-seq:

ios.printf(format_string [, obj, ...])    -> nil

Formats and writes to ios, converting parameters under control of the format string. See sprintf for details of the format string.

$stdout.printf "Number: %5.2f,\nString: %s\n", 1.23, "hello"
Number:  1.23,
String: hello


280
281
282
283
# File 'mrbgems/mruby-io/mrblib/io.rb', line 280

def printf(*args)
  write sprintf(*args)
  nil
end

#rewindObject

call-seq:

ios.rewind    -> 0

Positions ios to the beginning of input, resetting lineno to zero.

f = File.new("testfile")
f.readline   #=> "This is line one\n"
f.rewind     #=> 0
f.lineno     #=> 0
f.readline   #=> "This is line one\n"


193
194
195
# File 'mrbgems/mruby-io/mrblib/io.rb', line 193

def rewind
  seek(0, SEEK_SET)
end