Rob
Rob

Reputation: 33

Ruby HTML Entity Problem

New to Ruby and am wondering how to get the following to print just the degree symbol...

require 'htmlentities'
coder = HTMLEntities.new
puts coder.decode('°')

Currently the command line (Windows) output is: °

Thanks!

Upvotes: 0

Views: 926

Answers (1)

Xavier Holt
Xavier Holt

Reputation: 14619

It looks like HTMLEntities.decode returns a string in UTF-8, and your console is choking on that encoding. You'll have to re-encode your string before passing it to puts.

If you're using Ruby 1.9.2, it looks like the code is fairly straightforward (based on the String and Encoding documentation):

puts coder.decode('&deg;').encode(Encoding.find('<Whatever-Windows-Uses>'))

You might have to try a couple different encodings before you find something your console can understand.

If you're on an older version of Ruby, it looks like the re-encoding is doable through Iconv (see this question - I suspect you're just going in the opposite direction).

Hope this helps!

Upvotes: 1

Related Questions