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 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")

```
You can use `%q{}` 
 %q{"this is a string\n"}
 => "\"this is a string\n\""

This way it will escape the string characters.

From the [documentation](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-perl/):

> 
> There’s a general delimited string and regex quoting syntax similar to
>  Perl’s. It looks like %q{this} (single-quoted), or %Q{this}
>  (double-quoted), and %w{this for a single-quoted list of words}. You
>  %Q|can| %Q(use) %Q^other^ delimiters if you like.
> 
> 
> 

**EDIT:**
Since I misunderstand the question, I think your best option, if you do not want to rely on third-party libs (like JSON), is to replace with proper values:

> 
> str.gsub(/(\n)+/,"\n")
> 
> 
>