Skip to content

Instantly share code, notes, and snippets.

@advorak
Last active August 29, 2015 14:04
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 advorak/38647fa0752399beaf64 to your computer and use it in GitHub Desktop.
Save advorak/38647fa0752399beaf64 to your computer and use it in GitHub Desktop.
# I am executing a program 'umessage' and piping output to this
# ruby script ...
#
# The problem is that I'm noticing my program isn't receiving all
# of the output I expect it to receive from STDOUT to STDIN......
# It is consistently skipping data... any hints as to what I'm
# doing wrong?
DEBUG = true
@user_id = 5
@current_data = ''
while(data = $stdin.sysread(100).to_s)
sleep 0.25 if data.blank?
data = @current_data.to_s + data
@current_data = ''
messages = data.lines.slice_before(/^\d{2}\/\d{2}/).collect(&:join)
messages.each do |message|
if [message].join.match(/\n$/)
UMessage.create user_id: @user_id, data: message
else
@current_data = [message].join
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment