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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:18:02+00:00 2026-05-30T16:18:02+00:00

I have an email template where the user can enter text like this: Hello

  • 0

I have an email template where the user can enter text like this:

Hello {first_name}, how are you?

and when the email actually gets sent it replaces the placeholder text {first_name} with the actual value.

There will be several of these placeholders, though, and I wasn’t sure that gsub is meant to be used like this.

body = @email.body.gsub("{first_name}", @person.first_name)gsub("{last_name}", @person.last_name).gsub("",...).gsub("",...).gsub("",...).etc...

Is there a cleaner solution to achieving this functionality? Also, if anyone’s done something similar to this, did they find that they eventually hit a point where using multiple gsubs on a few paragraphs for hundreds of emails was just too slow?

EDIT

I ran some tests comparing multiple gsubs vs using regex and it came out that the regex was usually 3x FASTER than using multiple gsubs. However, I think the regex code is a littler harder to read as-is, so I’m going to have to clean it up a bit but it does indeed seem that using regex is significantly faster than multiple gsubs. Since my use case will involve multiple substitutions for a large number of documents, the faster solution is better for me, even though I’ll have to add a little more documentation.

  • 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-30T16:18:04+00:00Added an answer on May 30, 2026 at 4:18 pm

    You have to put in regular expressions all strings you want to catch and in the hash you put the replacement of all catches:

    "123456789".gsub /(123|456)/, "123" => "ABC",
                                  "456" => "DEF"
    

    This code only works for ruby 1.9.

    If you can use a template library like erb or haml, they are the proper tool for this kind of task.

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

Sidebar

Related Questions

I'm trying to create a user control template that I can send as email.
I have email addresses like user1@gmail.com , user2@ymail.com user3@hotmail.com ... etc. I want a
I'm programming in PHP and would like to create web pages which have email
We have an email confirmation page for registration which can be hit with a
I have a JSON format like this returned via a ajax call { user_page_id:36,
I have a custom CreateUserWizard in which the user gives his email as username.
I have created an email template in Ax, using the %varname% as a placeholder
I have applied a template filter, which extracts the domain from an email address.
I have a django template which I have to send as email body, the
I have a user controller that checks to login a user. It has this

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.