Skip to content

Instantly share code, notes, and snippets.

@Servana
Created February 25, 2011 18:42
Show Gist options
  • Save Servana/844274 to your computer and use it in GitHub Desktop.
Save Servana/844274 to your computer and use it in GitHub Desktop.
STDIN JSON parser
require 'rubygems'
require 'fcntl'
require 'json'
block = ""
# Set $stdin to be non-blocking
$stdin.fcntl(Fcntl::F_SETFL,Fcntl::O_NONBLOCK)
begin
$stdin.each_line { |line|
block << line
}
json = JSON.parse(block)
print json['data'].unpack("m")
rescue Errno::EAGAIN
# Remove this thread since we won't be reading from $stdin
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment