Ruby write to a file binary




















That's right - Ruby was meant for other stuff probably, but we have a pretty big Rails application already and we just faced this need to operate with binary files. See stackoverflow. As this question pops up as top google result when searching for "ruby write binary", and this answer is actually showed as relevant blurb directly in the search results: Ruby is perfectly fine for what OP laid out.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked 4. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.

You can achieve the same thing by setting the encoding explicitly with s. This is key if you want to read binary into a string and move them around e. Since IO is the parent class of File , you could also do the following which is probably more expressive:.

On Unix-like platforms, there is no difference between opening files in "binary" and "text" modes. On Windows, "text" mode converts line breaks to DOS style, and "binary" mode does not. Unless you need linebreak conversion on Windows platforms, just open all the files in "binary" mode. There is no harm in reading a text file in "binary" mode. If you really want to distinguish, you will have to match File.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Automatically open a file as binary with Ruby Ask Question. Asked 9 years, 11 months ago. Active 4 years, 3 months ago. Viewed 21k times. Improve this question.

Collectives on Stack Overflow. Learn more. How to write a Tempfile as binary Ask Question. Asked 1 year, 9 months ago. Active 1 year, 9 months ago. Viewed 4k times. Writing regular File you would be able to do this as following: File.

Improve this question. Gijs Paulides Gijs Paulides 1, 11 11 silver badges 27 27 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Stefan Stefan k 11 11 gold badges silver badges bronze badges. From the docs : "[



0コメント

  • 1000 / 1000