Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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!

share|improve this answer
    
Thank you. It works with iso-8859-1 –  Rob Apr 18 '11 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.