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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:13:43+00:00 2026-05-26T22:13:43+00:00

Is it possible to use variables in both fields of the gsub method ?

  • 0

Is it possible to use variables in both fields of the gsub method ?
I’m trying to get this piece of code work :

$I = 0
def random_image
  $I.to_s
  random = rand(1).to_s
  logo = File.read('logo-standart.txt')
  logo_aleatoire = logo.gsub(/#{$I}/, random)
  File.open('logo-standart.txt', "w") {|file| File.puts logo_aleatoire}
  $I.to_i
  $I += 1
end

Thanks in advance !

  • 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-26T22:13:43+00:00Added an answer on May 26, 2026 at 10:13 pm
    filecontents = File.read('logo-standart.txt')
    filecontents.gsub!(/\d+/){rand(100)}
    File.open("logo-standart.txt","w"){|f| f << filecontents }
    

    The magic line is the second line.

    • The gsub! function modifies the string in-place, unlike the gsub function, which would return a new string and leave the first string unmodified.
    • The single parameter that I passed to gsub! is the pattern to match. Here, the goal is to match any string of one or more digits — this is the number that you’re going to replace. There’s no need to loop through all of the possible numbers running gsub on each one. You can even match numbers as high as a googol (or higher) without your program taking longer and longer to run.
    • The block that gsub! takes is evaluated each time the pattern matches to programmatically generate a replacement number. So each time, you get a different random number. This is different from the more usual form of gsub! that takes two parameters — there the parameter is evaluated once before any pattern matching occurs, and all matches are replaced by the same string.

    Note that the way this is structured, you get a new random number for each match. So if the number 307 appears twice, it turns into two different random numbers.

    If you wanted to map 307 to the same random number each time, you could do the following:

    filecontents = File.read('logo-standart.txt')
    randomnumbers = Hash.new{|h,k| h[k]=rand(100)}
    filecontents.gsub!(/\d+/){|match| randomnumbers[match]}
    File.open("logo-standart.txt","w"){|f| f << filecontents }
    

    Here, randomnumbers is a hash that lets you look up the numbers and find what random number they correspond to. The block passed when constructing the hash tells the hash what to do when it finds a number that it hasn’t seen before — in this case, generate a new random number, and remember what that random number the mapping. So gsub!‘s block just asks the hash to map numbers for it, and randomnumbers takes care of generating a new random number when you encounter a new number from the original file.

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

Sidebar

Related Questions

Is it possible to get the value from a JavaScript variable and use this
Is it possible to use a variable from one page in a piece of
I'm using R 2.8.1 and it is possible to use both = and <-
I know it is possible to use a variable as a variable name for
Is it possible to use EventListener to Listen to a variable and detect when
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
I have recently started looking into Google Charts API for possible use within the
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is possible to use ArcSDE API in .NET(C#) ? http://edndoc.esri.com/arcsde/9.2/api/capi/dbconnects/dbconnects.htm Thanks
Is it possible to use a flash document embedded in HTML as a link?

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.