Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:40+00:00 2026-05-23T15:25:40+00:00

So, I have puts test\\nstring.gsub(/\\n/, \n) and that works. But how do I write

  • 0

So, I have

puts "test\\nstring".gsub(/\\n/, "\n")

and that works.

But how do I write one statement that replaces \n, \r, and \t with their correctly escaped counterparts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:25:41+00:00Added an answer on May 23, 2026 at 3:25 pm

    Those aren’t escaped characters, those are literal characters that are only represented as being escaped so they’re human readable. What you need to do is this:

    escapes = {
      'n' => "\n",
      'r' => "\r",
      't' => "\t"
    }
    
    "test\\nstring".gsub(/\\([nrt])/) { escapes[$1] }
    # => "test\nstring"
    

    You will have to add other escape characters as required, and this still won’t accommodate some of the more obscure ones if you really need to interpret them all. A potentially dangerous but really simple solution is to just eval it:

    eval("test\\nstring")
    

    So long as you can be assured that your input stream doesn’t contain things like #{ ... } that would allow injecting arbitrary Ruby, which is possible if this is a one shot repair to fix some damaged encoding, this would be fine.

    Update

    There might be a mis-understanding as to what these backslashes are. Here’s an example:

    "\n".bytes.to_a
    # => [10]
    
    "\\n".bytes.to_a
    # => [92, 110]
    

    You can see these are two entirely different things. \n is a representation of ASCII character 10, a linefeed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have feeling, that if one defines a method def test puts 'Hi' end
I have some code I need to write a test for that connects to
i have the following class test hash={} def printHash puts hash[1] puts hash[2] puts
I have written a small app that puts my bluetooth in discoverable mode for
I have a process where a user puts in a comma delimited list that
I have a custom XML schema defined for page display that puts elements on
I have a script that reads a form and puts some info into a
I have to build a C# program that makes CSV files and puts long
I have found the Form.TopMost property but it puts the form on top of
I have an app that uses the iPhone camera and puts an overlay over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.