Best practices for handling binary data in Ruby?

Posted by StackedCrooked on Stack Overflow See other posts from Stack Overflow or by StackedCrooked
Published on 2010-04-16T07:56:43Z Indexed on 2010/04/16 8:53 UTC
Read the original article Hit count: 182

Filed under:

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?

© Stack Overflow or respective owner

Related posts about ruby