0

I have a yaml file with a pound-sterling sign on it -

amount: "£50"

when I access the symbol it return the following:

"£50"

I am using hashie:mash to load and access my yaml... ideas are welcome, haven't found anything on the webs that give a straight forward solution (or at least one that works for me)

Pippo
  • 905
  • 11
  • 22
  • see if you can find your answer [here](http://stackoverflow.com/questions/1158072/ruby-to-yaml-utf8-string) – Uri Agassi Nov 24 '14 at 19:12
  • Where is it returning that value *to*? It looks like you're trying to output a UTF-8 character as US-ASCII; Ruby strings are UTF-8 by default. What are `Encoding.default_internal` and `Encoding.default_external` set to? – Chris Heald Nov 24 '14 at 19:13
  • see if you can find your answer [here](http://stackoverflow.com/questions/1158072/ruby-to-yaml-utf8-string) – Uri Agassi Nov 24 '14 at 19:13
  • external is CP850, internal is nil – Pippo Nov 24 '14 at 19:19

1 Answers1

1

The external encoding is your issue; Ruby is assuming that any data read from external files is CP-850, rather than UTF-8.

You can solve this a few ways:

  1. Set Encoding.default_external ='utf-8'. This will tell Ruby to read files as UTF-8 by default.
  2. Explicitly read your file as UTF-8, via open('file.yml', 'r:utf-8')
  3. Convert your string to UTF-8 before you pass it to your YAML parser:

You can do this via String#force_encoding, which tells Ruby to reinterpret the raw bytes with a different encoding:

 text = open("file.yml").read
 text.force_encoding("utf-8")
 YAML.load text
Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • Encoding.default_external ='utf-8' did the trick! ty so much!!! It is weird tho why, I am using the exact same version of gems/ruby but Ubuntu this does not occur but moving to windows I have to explicitly state the line above... either way, it looks good! thanks again!! – Pippo Nov 25 '14 at 10:52