Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Last active December 24, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/6767508 to your computer and use it in GitHub Desktop.
Save tenderlove/6767508 to your computer and use it in GitHub Desktop.
require 'psych'
require 'thread'
class PullParser
def initialize io
@queue = SizedQueue.new 10
parser = Psych::Parser.new self
@thread = Thread.new {
begin
parser.parse io
ensure
@queue.push nil
end
}
end
def next_token
@queue.pop
end
def finish
@thread.join
end
private
def method_missing name, *args
@queue << [name, args]
end
end
pp = PullParser.new DATA
while token = pp.next_token
p token
# do something
end
pp.finish
__END__
---
- foo
- foo
- foo
- foo
- foo
- foo
- foo
- foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment