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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:49:47+00:00 2026-05-28T22:49:47+00:00

I want ‘This Is A 101 Test’ to be ‘This Is A Test’, but

  • 0

I want ‘This Is A 101 Test’ to be ‘This Is A Test’, but I can’t get the syntax right.

src = 'This Is A 101 Test'
puts "A) " + src                       # base => "This Is A 101 Test"
puts "B) " + src[/([a-z]+)/]           # only does first word => "his"
puts "C) " + src.gsub!(/\D/, "")       # Does digits, I want alphabetic => "101"
puts "D) " + src.gsub!(/\W///g)        # Nothing. => ""
puts "E) " + src.gsub(/(\W|\d)/, "")   # Nothing. => ""
  • 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-28T22:49:48+00:00Added an answer on May 28, 2026 at 10:49 pm

    First off, you need to be careful with gsub and gsub!. The latter is “dangerous!” and will modify the value of src. If you’re executing these statements in order, be aware that a.gsub!(/a/, "b") and a = a.gsub(/a/, "b") will both do the same thing to a. Part of the issue with your code is that src is being modified.

    The B method returns "his" but makes no changes to source

    src[/([a-z]+)/]     # => "his"
    src                 # => "This Is A 101 Test"
    

    The C method removes all characters that aren’t numbers:

    src.gsub!(/\D/, "") # => "101"
    src                 # => "101"
    

    The D method doesn’t work because the syntax is wrong. The gsub method accepts a regular expression/string to search and then a string to use for replacement. If you try it in IRB it will act as though you need another / somewhere.

    The E method replaces all non-word characters and all numbers:

    src.gsub(/(\W|\d)/, "") # => "This Is A  Test" (note the two spaces)
    src                     # => "This Is A 101 Test"
    

    You point out that it’s returning "". Well, what’s actually happening is that C and D as listed (with syntax issues fixed) are destructive changes. (Also, if run on "101", D will actually return nil as no substitutions were performed.) So E is just being run on "101", and since you’re replacing all non-words and all numbers with "", it becomes "101".


    The answer you’re looking for would be something like:

    src.gsub!(/\d\s?/, "") # => "This Is A Test"
    src                    # => "This Is A Test"
    

    And my favorite for dealing with all scenarios of double spaces (because squeeze is quite efficient at combining like characters, strip is quite efficient at stripping trailing whitespace, and those ! return nil if they make no replacements):

    src = src.gsub(/\d+/, "").squeeze(" ").strip
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to make sure I got that right: Base SDK = What you're building
want to have a Hyperlink-Button in a gridView in which I can display a
Want the function to sort the table by HP but if duplicate HPs then
want to ask user to input something but not want to wait forever. There
Want to verify that my understanding of how this works. Have a C++ Class
Want to attach an Event Handler/Listener to some links using the delegate() method, but
Want to just confirm on following. Please verify if this is correct: 1. As
Want to write a typing test application (GUI, windows). Is simple terms. app shows
Want i want to do is i get a number from a php script.
want my table to show up only half way from the bottom of the

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.