Best practices for handling binary data in Ruby?
- by StackedCrooked
What are the best practices for reading and writing binary data in Ruby?
In the code sample below I needed to send a binary file using over HTTP (as POST data):
f = File.new("resp.der", "r") # binary file
begin
while true
out.syswrite(f.sysread(1)) # out is an output stream (type IO)
end
rescue EOFError => err
puts "Sent response."
end
While this code seems to do a good job, it probably isn't very idiomatic. How can I improve it?