Change Ruby 1.9 file encoding mid read

A

Adam Salter

[Note: parts of this message were removed to make it a legal post.]

Is it possible to change a file's encoding mid read?

Trying do do something along the lines of the following:

file = File.open('testfile.rb', 'r:US-ASCII')
file.each_line do |line|
if (file.lineno == 1 || file.lineno == 2) && line =~ /
^#.*coding:\s([\w-]+)/
file.force_encoding($1)
end
next if line.valid_encoding?
puts "bad encoding: [%s:%d]" % [file.path, file.lineno]
end


-Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top