Class: Complate::Stream

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/complate/stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Stream

Returns a new instance of Stream.



3
4
5
6
# File 'lib/complate/stream.rb', line 3

def initialize(&block)
  @after_start = block
  @buf = ''
end

Instance Method Details

#each(&block) ⇒ Object



16
17
18
19
20
# File 'lib/complate/stream.rb', line 16

def each(&block)
  @block = block
  @after_start.call(self)
  flush
end

#flush(*_) ⇒ Object



22
23
24
25
26
27
# File 'lib/complate/stream.rb', line 22

def flush(*_)
  if @block && @buf != ''
    @block.call(@buf)
    @buf = ''
  end
end

#write(str) ⇒ Object



12
13
14
# File 'lib/complate/stream.rb', line 12

def write(str)
  @buf << str
end

#writeln(str) ⇒ Object



8
9
10
# File 'lib/complate/stream.rb', line 8

def writeln(str)
  write("#{str}\n")
end