yajl-ruby icon indicating copy to clipboard operation
yajl-ruby copied to clipboard

Block parameters for Parser.new and Encoder.new

Open padde opened this issue 10 years ago • 0 comments

Just wanted to sprinkle some syntactic sugar on the Parser and Encoder classes. Whereas now you would have to write

parser = Yajl::Parser.new
parser.on_parse_complete = lambda { |hash|
  # do something with hash
}

and

encoder = Yajl::Encoder.new
encoder.on_progress_callback = lambda { |str|
  # do something with str
}

this branch allows you to write

parser = Yajl::Parser.new do |hash|
  # do something with hash
end

and

encoder = Yajl::Encoder.new do |str|
  # do something with str
end

While I was at it, I

  • fixed some typos and indentation in the comments
  • found and fixed a bug that rendered on_progress_callback practically useless

The bug occurs because Encoder#encode takes a block parameter which sets on_progress_callback in the background. However, later on the block is called directly instead of referring to the on_progress_callback. This means that setting on_progress_callback before calling encode had no effect. This probably went unnoticed because there was no spec for that case but there is now. Check out https://github.com/padde/yajl-ruby/commit/9ab456e1f29e6a685fb81ae91f704ff5a0238d84.

Please comment on my branch and even if you don't like the block parameters, consider cherry-picking the other fixes.

Cheers

padde avatar Jun 19 '14 15:06 padde