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 7831505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:45:52+00:00 2026-06-02T11:45:52+00:00

I have a simple text file with content: patent-inv I’m going to replace patent-inv

  • 0

I have a simple text file with content:

   patent-inv

I’m going to replace patent-inv with part1\\part2

I did it in such way:

source_file = IO.read("#{Rails.root}/my_userdata/1.txt")
source_file.gsub!('<<<patent-inv>>>', "part1\\\\part2")             
File.open("#{Rails.root}/my_userdata/2.txt", "wb") {|f| f.write source_file }

But in file I got:
part1\part2

I tried several ways, but I got the same result:

source_file.gsub!('<<<patent-inv>>>', "part1\\\\part2") => "part1\part2"
source_file.gsub!('<<<patent-inv>>>', "part1\\\part2") => "part1\part2"
source_file.gsub!('<<<patent-inv>>>', "part1\\part2") => "part1\part2"

Could Anyone help me find out this strange behavior?

  • 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-06-02T11:45:54+00:00Added an answer on June 2, 2026 at 11:45 am

    The following works for me. Have you tried it in the terminal? It could be unescaped when you output the result.

    > a = "<<<patent-inv>>>"
    > a.gsub("<<<patent-inv>>>", "part1\\\\part2")    
    => "part1\\part2" 
    

    You can see how the output varies between puts and p

    > puts a
    part1\part2
    > p a
    "part1\\part2"
    > puts a.inspect
    "part1\\part2"
    

    EDIT Here is the output displayed as you want it.

    1.9.2-p290 :037 > a.gsub!("<<<patent-inv>>>", "part1\\\\\\part2")
     => "part1\\\\part2" 
    > p a
    "part1\\\\part2"
     => "part1\\\\part2" 
    > puts a
    part1\\part2
     => nil 
    

    We know that 4 backslashes when changed equals 2 backslashes when unescaped. So 4 backslashes need to be generated AFTER the substitution for it to be displayed correctly.

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

Sidebar

Related Questions

I have a simple text file, that has following content word1 word2 I need
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have a very simple python script that should scan a text file, which
Lets say i have a text file with following content: Hello! How are you?
I have a simple text file containing some CSV with the following structure: @Parent1_Field1,
I have a simple text file with extension .txt added to my solution with
I need simple program in C++ that will open text file and going line
I have three simple html or php file that I'm going to create. What
Problem I have a file which contains such text (nothing else) 2010-10-05 I would
I have a simple layout file which displays the icons and some text view

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.