Module: ImplIOO

Included in:
IOO, IOOTCPSocket
Defined in:
lib/ioo.rb

Overview

With this class you can easily overwrite only write or read methods to change the whole IO.

This is very usefull to produce a crypted IO for example.

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



16
17
18
19
# File 'lib/ioo.rb', line 16

def << ( obj )
  write obj
  self
end

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



21
22
23
24
25
26
27
# File 'lib/ioo.rb', line 21

def each ( sep_string=$/, &block )
  begin
    loop { block[readline(sep_string)] }
  rescue EOFError
    return self
  end
end

#each_byte(&block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/ioo.rb', line 30

def each_byte ( &block )
  begin
    loop { block[readchar] }
  rescue EOFError
    return nil
  end
end

#getcObject



38
39
40
41
42
43
44
# File 'lib/ioo.rb', line 38

def getc
  begin
    readchar
  rescue EOFError
    nil
  end
end

#gets(sep_string = $/) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/ioo.rb', line 46

def gets ( sep_string=$/ )
  begin
    readline(sep_string)
  rescue EOFError
    nil
  end
end


54
55
56
57
58
59
60
61
# File 'lib/ioo.rb', line 54

def print ( *args )
  if args.empty?
    write $_
  else
    args.each { |x| write x }
  end
  nil
end

#printf(fmt, *args) ⇒ Object



63
64
65
66
# File 'lib/ioo.rb', line 63

def printf ( fmt, *args )
  write Kernel.sprintf(fmt, *args)
  nil
end

#putc(c) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ioo.rb', line 68

def putc ( c )
  case c
  when Numeric then write c.chr
  when String
    if c.size == 1
      write c
    else
      c = c[0].chr
      write c
    end
  else
    c = c.to_s[0].chr
    write c
  end
  c
end

#puts(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ioo.rb', line 85

def puts ( *args )
  if args.size == 0
    write "\n"
  else
    args.each do |x|
      x = x.to_s
      write x
      write "\n" unless x[-1] == ?\n
    end
  end
  nil
end

#readcharObject

Raises:

  • (EOFError)


98
99
100
101
# File 'lib/ioo.rb', line 98

def readchar
  raise EOFError if eof?
  read(1)[0]
end

#readline(sep_string = $/) ⇒ Object

Raises:

  • (EOFError)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ioo.rb', line 103

def readline ( sep_string=$/ )
  raise EOFError if eof?
  buf = ''
  begin
    while (char = readchar) != ?\n
      buf += char.chr
    end
    return buf + char.chr
  rescue EOFError
    return buf
  end
end

#readlines(sep_string = $/) ⇒ Object



116
117
118
119
120
# File 'lib/ioo.rb', line 116

def readlines ( sep_string=$/ )
  res = []
  each_line(sep_string) { |line| res << line }
  res
end