Posted 10 days ago. Visible to the public.

Ruby: How to load a file with a known encoding

In case Ruby does not detected the expected encoding of a file automatically you can specify the known encoding manually.

Example with File.open

Copy
file = File.open('some.bin', encoding: Encoding::ASCII_8BIT) text = file.read text.encoding => #<Encoding:ASCII-8BIT>

Example with File.read

Copy
text = File.read('some.bin', encoding: Encoding::ASCII_8BIT) text.encoding => #<Encoding:ASCII-8BIT>

More details about the encoding of strings in Ruby can be found here.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Emanuel De
Last edit:
10 days ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more