Thomas S.
Thomas S.

Reputation: 6345

Ruby: read file byte-wise

I'm absolutely new to Ruby having just read some tutorials. It looks like I can read the file content as

svg = File.read(path)

Now it looks like the file content is treated as a string and regular expression replace is used:

svg = svg.gsub(/(width|height)="\d+px"/, '')

How to define the encoding to use the text file? How to read the file contents into a byte array (leaving the regular expression aside)?

Upvotes: 1

Views: 1611

Answers (1)

Max
Max

Reputation: 22325

You want File#open. Rather than returning a string, this gives you an IO object on which you can call methods such as each_byte. You can also pass open arguments specifying the encoding.

Upvotes: 3

Related Questions