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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:17:15+00:00 2026-05-13T12:17:15+00:00

The following code defines a hash with regular expressions (keys) and replacements (values). Then

  • 0

The following code defines a hash with regular expressions (keys) and replacements (values). Then it iterates over the hash and replaces the string accordingly.

Simple string substitution works well, but when I need to compute the resut before substituting it (the case of years to days change), it does not. And it is key that the hash is defined beforehand.

What am I missing? Any help will be very appreciated.

a = "After 45 years we cannot use this thing."

hash = {
  /(\d+) years/ => "#{$1.to_f*2}" + ' days',
  /cannot/      => 'of course we CAN'  
}

hash.each {|k,v| 

  a.gsub!(k) { v }
}

puts a

Thanks!

  • 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-13T12:17:15+00:00Added an answer on May 13, 2026 at 12:17 pm

    String#gsub! has two forms, one in which you pass in a string as the second argument, in which variable references like $1 and $2 are replaced by the corresponding subexpression match, and one in which you pass in a block, which is called with arguments which have the subexpression matches passed in. You are using the block form when calling gsub!, but the string in your hash is attempting to use the form in which a string is passed in.

    Furthermore, the variable interpolation in your string is occurring before the match; variable interpolation happens as soon as the string is evaluated, which is at the time your hash is being constructed, while for this to work you would need variable interpolation to happen after the subexpression replacement happens (which is never the case; variable interpolation will happen first, and the resulting string would be passed in to gsub! for gsub! to substitute the subexpression match for $1, but $1 would have already been evaluated and no longer in the string, as the interpolation has already occurred).

    Now, how to fix this? Well, you probably want to store your blocks directly in the hash (so that the strings won’t be interpreted while constructing the hash, but instead when gsub! invokes the block), with an argument corresponding to the match, and $1, $2, etc. bound to the appropriate subexpression matches. In order to turn a block into a value that can be stored and later retrieved, you need to add lambda to it; then you can pass it in as a block again by prefixing it with &:

    hash = {
      /(\d+) years/ => lambda { "#{$1.to_f*2} days" },
      /cannot/      => lambda { 'of course we CAN' }
    }
    
    hash.each {|k,v|
      a.gsub!(k, &v)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently noticed the following code which basically defines a class method public Func<string,
I've got the following Delphi code that defines a set and then a simple
The following code defines a sequence of names that are mapped to numbers. It
I'm trying to translate the following JS code to ObjC; It basically defines a
I have the following code. // mfc.cpp : Defines the entry point for the
Consider the following code // BOGP.cpp : Defines the entry point for the console
I have a unit wich defines TBla and the following code in the initialize
The following code defines my Bitmap: Resources res = context.getResources(); mBackground = BitmapFactory.decodeResource(res, R.drawable.background);
The following code works fine #define open { #define close } #include<stdio.h> #define int
The following code in VS2008 gives me a variable is already defined warning: if

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.