0

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!

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.