0

Does Ruby have a method to unescape hex codes within a string?

For example:

  string = "Plus minus symbol: ±"

How can I print that string with the hex code replaced with the actual character: "±"? I'm looking for a generic solution that works for any hex code.

DanVoges
  • 9
  • 1
  • 3
  • I was able to decode the special character using HTMLEntities.new.decode(string). Thanks @theTinMan! – DanVoges May 16 '17 at 15:03

1 Answers1

1

You could use Nokogiri to parse the symbol and then add it to your string:

require 'nokogiri'

symbol = Nokogiri::HTML.parse("±").text
#=> "±"

string = "Plus minus symbol: #{symbol}"
#=> "Plus minus symbol: ±"
Gerry
  • 10,337
  • 3
  • 31
  • 40