I’m using Ruby 1.9 to open several files and copy them into an archive. Now there are some binary files, but some are not. Since Ruby 1.9 does not open binary files automatically as binaries, is there a way to open them automatically anyway? (So “.class” would be binary, “.txt” not)
Share
Actually, the previous answer by Alex D is incomplete. While it’s true that there is no “text” mode in Unix file systems, Ruby does make a difference between opening files in binary and non-binary mode:
is different from (note the
"rb")The latter, as the docs say, set the external encoding to ASCII-8BIT which tells Ruby to not attempt to interpret the result at UTF-8. You can achieve the same thing by setting the encoding explicitly with
s.force_encoding('ASCII-8BIT'). This is key if you want to read binary into a string and move them around (e.g. saving them to a database, etc.).