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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:09:07+00:00 2026-05-22T12:09:07+00:00

In How do I limit the number of replacements when using gsub? , someone

  • 0

In How do I limit the number of replacements when using gsub?, someone suggested the following way to do a limited number of substitutions:

str = 'aaaaaaaaaa'
count = 5
p str.gsub(/a/){if count.zero? then $& else count -= 1; 'x' end}
# => "xxxxxaaaaa"

It works, but the code mixes up how many times to substitute (5) with what the substitution should be (“x” if there should be a substitution, $& otherwise). Is it possible to seperate the two out?

(If it’s too hard to seperate the two things out in this scenario, but it can be done in some other scenarios, post that as an answer)

  • 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-22T12:09:08+00:00Added an answer on May 22, 2026 at 12:09 pm

    How about just extracting the replacement as an argument and encapsulating the counter by having the block close over it inside a method?

    str = "aaaaaaaaaaaaaaa"
    
    def replacements(replacement, limit)
        count = limit
        lambda { |original| if count.zero? then original else count -= 1; replacement end }
    end
    
    p str.gsub(/a/, &replacements("x", 5))
    

    You can make it even more general by using a block for the replacement:

    def limit(n, &block)
        count = n
        lambda do |original|
            if count.zero? then original else count -= 1; block.call(original) end
        end
    end
    

    Now you can do stuff like

    p str.gsub(/a/, &limit(5) { "x" })
    p str.gsub(/a/, &limit(5, &:upcase))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you limit the number of replacements made by String#gsub in Ruby? In
I'm trying to limit the number of connections my server will accept using semaphores,
Is there a way to limit the number of rows in a SQL Server
Is there an easy way to limit the number of times a view can
Is there a way to turn off multithreading or limit the number of cores
Is there a way to limit the number of selections a user can choose
Is there a way to limit the number of instances of a rake task?
How do I limit the number of panels shown on a single page using
I am looking for a way to limit SmtpAppender to limit number of emails
I try to limit the number of allowable characters to 5 by using try

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.