Class: JsonEmitter::Stream
- Inherits:
-
Object
- Object
- JsonEmitter::Stream
- Includes:
- Enumerable
- Defined in:
- lib/json-emitter/stream.rb
Overview
Represents a stream of JSON to be generated and yielded. It can be treated like any Enumerable. Unlike UnbufferedStream, the size of the yielded strings can vary from 1 to 1000’s.
Instance Method Summary collapse
-
#each ⇒ Enumerator
If a block is given, each chunk of JSON is yielded to it.
-
#initialize(enum) ⇒ Stream
constructor
Initialize a new stream.
-
#write(io) ⇒ Object
Write the stream to the specified IO object.
Constructor Details
#initialize(enum) ⇒ Stream
Initialize a new stream.
14 15 16 |
# File 'lib/json-emitter/stream.rb', line 14 def initialize(enum) @enum = enum end |
Instance Method Details
#each ⇒ Enumerator
If a block is given, each chunk of JSON is yielded to it. If not block is given, an Enumerator is returned.
34 35 36 37 38 39 40 41 42 |
# File 'lib/json-emitter/stream.rb', line 34 def each if block_given? @enum.each { |str| yield str } else @enum end end |
#write(io) ⇒ Object
Write the stream to the specified IO object.
23 24 25 26 27 |
# File 'lib/json-emitter/stream.rb', line 23 def write(io) each { |str| io << str } end |