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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:22:01+00:00 2026-05-16T15:22:01+00:00

First, a working example: string = foo-bar-25-baz if string =~ /(.+)-(10|25)(?:-(baz))?/ puts $1 puts

  • 0

First, a working example:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $1 
  puts $2
  puts $3
end

This produces ‘foo-bar’, ’25’ and ‘baz’ on three lines, as expected. But if we do this:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $1.gsub('-', ' ') # Here be the problem
  puts $2 # nil
  puts $3 # nil
end

the values of $2 and $3 are now nil. I have to puts $2 and puts $3 and then $1.gsub(...), and it will work. As far as I can tell this only applies to gsub and gsub!

This causes the same problem:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $3.gsub('hard', 'h')
  puts $1 # nil
  puts $2 # nil
end

I spent about 15 minutes debugging this and I’m wondering why.

  • 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-16T15:22:02+00:00Added an answer on May 16, 2026 at 3:22 pm

    gsub is most likely re-assigning those variables (as would pretty much any other function that uses the regexp engine). If you need to call gsub before using all your original match results, store them to a local variable first with something like match_results = [$1, $2, $3].

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

Sidebar

Related Questions

UPDATE: This is the working example. First we create a class to hold weekday,
I am running this simple example with my Gmail account, but its not working
I'm working on my first JSON example in objective-c and came across this great
This is my first time working with a WPF datagrid. From what I understand
This is my first time working with file i/o in java, and it's not
I have found following working example of CGI with bash. If I change first
I have managed to get my first array working but no matter how many
This is my first attempt at C++, following an example to calculate a tip
I have three classes organized in following manner. Foo is a template class, Bar
For some weird reason, the codes below are first working, then website is redirecting

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.