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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:20:54+00:00 2026-05-15T06:20:54+00:00

Consider the following snippet: puts ‘hello’.gsub(/.+/, ‘\0 \\0 \\\0 \\\\0’) This prints ( as

  • 0

Consider the following snippet:

puts 'hello'.gsub(/.+/, '\0 \\0 \\\0 \\\\0')

This prints (as seen on ideone.com):

hello hello \0 \0

This was very surprising, because I’d expect to see something like this instead:

hello \0 \hello \\0

My argument is that \ is an escape character, so you write \\ to get a literal backslash, thus \\0 is a literal backslash \ followed by 0, etc. Obviously this is not how gsub is interpreting it, so can someone explain what’s going on?

And what do I have to do to get the replacement I want above?

  • 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-15T06:20:54+00:00Added an answer on May 15, 2026 at 6:20 am

    Escaping is limited when using single quotes rather then double quotes:

    puts 'sinlge\nquote'
    puts "double\nquote"
    

    "\0" is the null-character (used i.e. in C to determine the end of a string), where as '\0' is "\\0", therefore both 'hello'.gsub(/.+/, '\0') and 'hello'.gsub(/.+/, "\\0") return "hello", but 'hello'.gsub(/.+/, "\0") returns "\000". Now 'hello'.gsub(/.+/, '\\0') returning 'hello' is ruby trying to deal with programmers not keeping the difference between single and double quotes in mind. In fact, this has nothing to do with gsub: '\0' == "\\0" and '\\0' == "\\0". Following this logic, whatever you might think of it, this is how ruby sees the other strings: both '\\\0' and '\\\\0' equal "\\\\0", which (when printed) gives you \\0. As gsub uses \x for inserting match number x, you need a way to escape \x, which is \\x, or in its string representation: "\\\\x".

    Therefore the line

    puts 'hello'.gsub(/.+/, "\\0 \\\\0 \\\\\\0 \\\\\\\\0")
    

    indeed results in

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

Sidebar

Related Questions

Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider the following snippet: get '/hello/:name' do |n| Hello #{n}! end How can I
Consider the following code snippet: String input = Print this; System.out.println(input.matches(\\bthis\\b)); Output false What
Consider the following snippet: import lxml.html html = '<div><br />Hello text</div>' doc = lxml.html.fromstring(html)
Consider the following snippet: var t = <span>Hello world</span>; var range = window.getSelection().getRangeAt(0); range.deleteContents();
Consider the following snippet of code: foreach (var setting in RequiredSettings) { try {
Consider the following snippet of code: // get number of sheep in DataTable by
please consider the following snippet: SimpleDateFormat parser = new SimpleDateFormat(MMdd); parser.setLenient(false); try { Date
Consider the following code snippet: $beat = date('B'); // 1 beat = 86.4 seconds,
Consider the following HTML snippet. The desired effect is to have a dropdown be

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.