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

  • Home
  • SEARCH
  • 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 6344077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:34:06+00:00 2026-05-24T20:34:06+00:00

s = #main= ‘quotes’ s.gsub ‘, \\’ # => #main= quotes’quotes This seems to

  • 0
s = "#main= 'quotes'
s.gsub "'", "\\'" # => "#main= quotes'quotes"

This seems to be wrong, I expect to get "#main= \\'quotes\\'"

when I don’t use escape char, then it works as expected.

s.gsub "'", "*" # => "#main= *quotes*"

So there must be something to do with escaping.

Using ruby 1.9.2p290

I need to replace single quotes with back-slash and a quote.

Even more inconsistencies:

"\\'".length # => 2
"\\*".length # => 2

# As expected
"'".gsub("'", "\\*").length # => 2
"'a'".gsub("'", "\\*") # => "\\*a\\*" (length==5)

# WTF next:
"'".gsub("'", "\\'").length # => 0

# Doubling the content?
"'a'".gsub("'", "\\'") # => "a'a" (length==3)

What is going on here?

  • 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-24T20:34:06+00:00Added an answer on May 24, 2026 at 8:34 pm

    You’re getting tripped up by the specialness of \' inside a regular expression replacement string:

    \0, \1, \2, … \9, \&, \`, \', \+
    Substitutes the value matched by the nth grouped subexpression, or by the entire match, pre- or postmatch, or the highest group.

    So when you say "\\'", the double \\ becomes just a single backslash and the result is \' but that means “The string to the right of the last successful match.” If you want to replace single quotes with escaped single quotes, you need to escape more to get past the specialness of \':

    s.gsub("'", "\\\\'")
    

    Or avoid the toothpicks and use the block form:

    s.gsub("'") { |m| '\\' + m }
    

    You would run into similar issues if you were trying to escape backticks, a plus sign, or even a single digit.

    The overall lesson here is to prefer the block form of gsub for anything but the most trivial of substitutions.

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

Sidebar

Related Questions

main(int c,char **args){ int i char input[100]; bzero(input,100); for(i=1;i<c;i++) { input=strcat(input,args); input=strcat(input, ); }
int main() { int main=5; printf(%d,main); return 0; } In this case there is
main: #include QtGui/QApplication #include spc_login.h int main(int argc, char *argv[]) { QApplication a(argc, argv);
main() { char buff[10]; int a; for(int i=0; i<10;i++) { cin >> buff[i]; a=sizeof(buff[i]);
Main idea to put these question is get exact features of SQL Server 2008
Main purpose of this task is to calculate volumes and surface areas of three
I main issue is of authentication, then the actual copying of files from once
Main Problem : Get rid of # when user clicks on anchor tag. I
main.m #import <Cocoa/Cocoa.h> int main(int argc, char *argv[]) { return NSApplicationMain(argc, (const char **)
int main() { const int maxint=100;//The program will crash if this line is put

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.