Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby provide a way to do File.read() with specified encoding?

In ruby 1.9.x, we can specify the encoding with File.open('filename','r:iso-8859-1'). I often prefer to use a one-line File.read() if I am reading many short files into strings directly. Is there a way I can specify the encoding directly, or do I have to resort to one of the following?

str = File.read('filename') str.force_encoding('iso-8859-1') 

or

f = File.open('filename', 'r:iso-8859-1') s = '' while (line = f.gets)     s += line end f.close 
like image 643
Chthonic Project Avatar asked Jul 26 '12 07:07

Chthonic Project


People also ask

What is encoding in Ruby?

In Ruby, texts are encoded in UTF-8 by default. This is because UTF-8 is a multi-byte character encoding that allows a single character to take up between 1 and 4 bytes. Other encodings, such as UTF-7, UCS-2, UTF-16, etc., are also present.

What are the ruby file open modes?

Ruby allows the following open modes: "r" Read-only, starts at beginning of file (default mode). "r+" Read-write, starts at beginning of file. "w" Write-only, truncates existing file to zero length or creates a new file for writing.

How can you read an entire file and get each line in Ruby?

Use File#readlines to Read Lines of a File in Ruby File#readlines takes a filename to read and returns an array of lines. Newline character \n may be included in each line. We must be cautious when working with a large file, File#readlines will read all lines at once and load them into memory.


1 Answers

From the fine manual:

read(name, [length [, offset]], open_args) → string

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.

If the last argument is a hash, it specifies option for internal open().

So you can say things like this:

s = File.read('pancakes', :encoding => 'iso-8859-1') s.encoding #<Encoding:ISO-8859-1> 
like image 54
mu is too short Avatar answered Oct 02 '22 13:10

mu is too short