jeudi 30 avril 2015

file reading difference on ruby and mac os due to different encoding

I have a file test.conf on mac . I downloaded it from web location as application/octet. When I check fro mime type using commandline I get

     file -Ib ~/test.conf 
     text/plain; charset=us-ascii

But in ruby irb, i see it as utf-8 irb(main):039:0> f = File.open("test.conf") => # irb(main):040:0> f.external_encoding => # irb(main):041:0> contents = f.read irb(main):042:0> contents.encoding => #

So when I try to open the file for reading one line at a time using File.foreach in Ruby , I get 52 lines, whereas

    cat ~/test.conf | wc -l
    12

So my dilemma here is how to make sure ruby sees the same number of lines as cat command?

Regards

Aucun commentaire:

Enregistrer un commentaire