Change Ruby 1.9 file encoding mid read

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

On Sep 9, 2009, at 9:38 PM, Adam S. wrote:

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

Yes, try the set_encoding() method.

James Edward G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs