Question: I am reading a string from a file in ruby. This string has escape sequences in them that I would like turned into the appropriate characters.

For example I might have a string like this:

```
"this is a string\n"

```

With the actual characters `\` and `n` in the file, not the newline character.

Is there a built in function to decode these literals? Or will I have to write something to do it manually?

Note that I'm not using rails, and the file data is not json.

Answer: 
If you are satisfied with the escape sequences supported by the JSON format, you can do something like this:

```
require 'json'

def unescape(str)
  JSON.parse("[\"#{str}\"]")[0]
end

p unescape("\\nline\\u0040")          # "\nline@"

```

Please note that the ruby JSON parser does not accept naked values, so the string needs to be wrapped in an array or an object.

The JSON parser will also raise errors if the string cannot be correctly parsed so you should add some error handling code.
If this is just a plain text file (e.g. not JSON, YAML, or another data format that has specific escape sequences) then those character sequences have no special meaning. Therefore, if you want to give them one in the context of your application you'll have to write the code to do that yourself. For newlines specifically, you could do something like this:

```
input.gsub!('\n', "\n")

```