Skip to content

Instantly share code, notes, and snippets.

@brain64bit
Last active February 21, 2019 19:30
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 brain64bit/2d8ae9d4ee3be190597601eeddbc4ee4 to your computer and use it in GitHub Desktop.
Save brain64bit/2d8ae9d4ee3be190597601eeddbc4ee4 to your computer and use it in GitHub Desktop.
Ruby IO Writing & Slurping

Writing a file using IO

producer will write a single line of string to file every 1s

require 'securerandom'

fd = IO.sysopen("/tmp/lala.log", "w+")
writer = IO.new(fd)
writer.sync = true
i = 1
while true do
  writer.puts "#{i} - #{SecureRandom.uuid}"
  sleep 1
  i += 1
end
writer.close

Slurping a file using IO

subscriber will read a file every 3s

fd = IO.sysopen("/tmp/lala.log")
reader = IO.new(fd)
while (row = reader.gets) != nil do
  data = row.chomp
  puts ">>> #{data}"
  if reader.eof?
    puts "--------"
    sleep 3
  end
end
reader.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment