From: James Gray Date: 2009-08-07T23:58:04+09:00 Subject: Re: R1.9 mixed encoding in file On Aug 7, 2009, at 9:47 AM, Vít Ondruch wrote: > James Gray wrote: >> On Aug 7, 2009, at 8:49 AM, Vít Ondruch wrote: >> >>> Hello >> >> Hello. >> >>> C:\enc>echo p 'zufällige_žluťoučký'.encoding > encoding.rb >>> according to specified encoding. >> The problem with an idea like this is that before your String is ever >> created the code to create it must be read (correctly) by Ruby's >> parser and formed into a proper String literal. That would be >> impossible to do if String literals could be in any random Encoding. > > Yes, I understand that you have to parse the file. However, if I am > right, you still have to read the file binary in case you are looking > for some encoding directive on top of file. You don't really have to: $ cat source_encoding.rb # encoding: UTF-8 output = "" open(__FILE__, "r:US-ASCII") do |source| first_line = source.gets if first_line =~ /coding:\s*(\S+)/ source.set_encoding($1) else output << first_line end output << source.read end p [output.encoding, output[0...20] + "…"] $ ruby_dev source_encoding.rb [#, "\noutput = \"\"\nopen(__…"] James Edward Gray II