Erickson  Dyeggo Takano Erickson Dyeggo Takano - 1 month ago 14
Ruby Question

String convertions in Ruby

I'm kinda new on Ruby and I having some trouble working with string convertions. I have this input in UTF-8

"O João e o pé de feijão"


and I need a output like this

"O João e o pé de feijão"


in ISO-8859-1.

I tried to use the encode method, but, although it did converted to ISO-8859-1,
the output was

"O Jo\xC3\xA3o e o p\xC3\xA9 de feij\xC3\xA3o\nEssa \xC3\xA9 uma est\xC3\xB3ria que fez parte da infncia das crian\xC3\xA7as nascidas nos anos 70 e\n80."

Answer

You can use the HTMLEntities gem.

> gem install htmlentities
Fetching: htmlentities-4.3.4.gem (100%)
Successfully installed htmlentities-4.3.4
Parsing documentation for htmlentities-4.3.4
Installing ri documentation for htmlentities-4.3.4
Done installing documentation for htmlentities after 0 seconds
1 gem installed

Example:

require 'htmlentities'
HTMLEntities.new.encode("O João e o pé de feijão", :named)
#=> "O João e o pé de feijão"
Comments