Class: StringOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/tmail-pure/stringio.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = '') ⇒ StringOutput

Returns a new instance of StringOutput.



173
174
175
176
# File 'lib/tmail-pure/stringio.rb', line 173

def initialize(str = '')
  @dest = str
  @closed = false
end

Class Method Details

.new(str = '') ⇒ Object Also known as: open



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tmail-pure/stringio.rb', line 157

def new(str = '')
  if block_given?
    begin
      f = super
      yield f
    ensure
      f.close if f
    end
  else
    super
  end
end

Instance Method Details

#<<(str) ⇒ Object



244
245
246
247
248
# File 'lib/tmail-pure/stringio.rb', line 244

def <<(str)
  stream_check!
  @dest << str.to_s
  self
end

#closeObject



178
179
180
# File 'lib/tmail-pure/stringio.rb', line 178

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/tmail-pure/stringio.rb', line 182

def closed?
  @closed
end

#inspectObject



199
200
201
# File 'lib/tmail-pure/stringio.rb', line 199

def inspect
  "#<#{self.class}:#{@dest ? 'open' : 'closed'},#{id}>"
end

Raises:

  • (ArgumentError)


203
204
205
206
207
208
209
210
211
# File 'lib/tmail-pure/stringio.rb', line 203

def print(*args)
  stream_check!
  raise ArgumentError, 'wrong # of argument (0 for >1)' if args.empty?
  args.each do |s|
    raise ArgumentError, 'nil not allowed' if s.nil?
    @dest << s.to_s
  end
  nil
end

#printf(*args) ⇒ Object



229
230
231
232
233
# File 'lib/tmail-pure/stringio.rb', line 229

def printf(*args)
  stream_check!
  @dest << sprintf(*args)
  nil
end

#putc(ch) ⇒ Object



223
224
225
226
227
# File 'lib/tmail-pure/stringio.rb', line 223

def putc(ch)
  stream_check!
  @dest << ch.chr
  nil
end

#puts(*args) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/tmail-pure/stringio.rb', line 213

def puts(*args)
  stream_check!
  args.each do |str|
    @dest << (s = str.to_s)
    @dest << "\n" unless s[-1] == ?\n
  end
  @dest << "\n" if args.empty?
  nil
end

#sizeObject Also known as: pos



193
194
195
# File 'lib/tmail-pure/stringio.rb', line 193

def size
  @dest.size
end

#stringObject Also known as: value, to_str



186
187
188
# File 'lib/tmail-pure/stringio.rb', line 186

def string
  @dest
end

#write(str) ⇒ Object Also known as: syswrite



235
236
237
238
239
240
# File 'lib/tmail-pure/stringio.rb', line 235

def write(str)
  stream_check!
  s = str.to_s
  @dest << s
  s.size
end