How change charset of file

Discussion in 'Ruby' started by hirogeek, Mar 6, 2014.

  1. hirogeek

    hirogeek Guest

    Hi,

    I try to create file with utf-8 charset.


    If I do

    File.open("test.xml", 'w:utf-8') { |f| f.write('<?xml version="1.0" encoding="UTF-8"?>') }

    file -I test.xml -> test.xml: application/xml; charset=us-ascii

    and I do

    File.open("test.xml", 'w:utf-8') { |f| f.write('<?xml version="1.0" encoding="UTF-8" é?>') }

    I add an utf-8 char (é)

    file -I test.xml -> test.xml: application/xml; charset=utf-8

    Ruby seem to be adaptative. But I would have alwayse charset utf-8. Can we do this ?

    I use ruby 2.1.1
     
    hirogeek, Mar 6, 2014
    #1
    1. Advertisements

  2. hirogeek

    Quintus Guest

    What you’re seeing is that UTF-8 is in fact 100% compatible with
    US-ASCII for all 7-bit-ASCII characters. There’s just no difference
    between the two encodings until you actually...
    ....add a non-ASCII character to your string.

    Vale,
    Marvin
     
    Quintus, Mar 16, 2014
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.