class Yajl::Encoder
def self.encode(obj, *args, &block)
:indent accepts a string and will be used as the indent character(s) during the pretty print process
:pretty accepts a boolean and will enable/disable "pretty printing" the resulting output
The +options+ hash allows you to set two encoding options - :pretty and :indent
If +io+ isn't passed, the resulting JSON string is returned. If +io+ is passed, nil is returned.
+io+ is the optional IO stream to encode the ruby object to.
+obj+ is a ruby object to encode to JSON format
output = Yajl::Encoder.encode(obj[, :pretty => true, :indent => "\t", &block])
Yajl::Encoder.encode(obj[, io, :pretty => true, :indent => "\t", &block])
Examples:
A helper method for encode-and-forget use-cases
def self.encode(obj, *args, &block) # TODO: this code smells, any ideas? args.flatten! options = {} io = nil args.each do |arg| if arg.is_a?(Hash) options = arg elsif arg.respond_to?(:write) io = arg end end if args.any? new(options).encode(obj, io, &block) end