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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:57:07+00:00 2026-06-15T18:57:07+00:00

I have a hash like: {:name => ‘foo’, :country => ‘bar’, :age => 22}

  • 0

I have a hash like:

{:name => 'foo', :country => 'bar', :age => 22}

I also have a string like

Hello ##name##, you are from ##country## and your age is ##age##. I like ##country## 

Using above hash, I want to parse this string and substitute the tags with corresponding values. So after parsing, the string will look like:

Hello foo, you are from bar and your age is 22. I like bar

Do you recommend taking help of regex to parse it? In that case if I have 5 values in the hash, then I would have to traverse through string 5 times and each time parsing one tag. I don’t think that is a good solution. Is there any better solution?

  • 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-06-15T18:57:09+00:00Added an answer on June 15, 2026 at 6:57 pm

    Here is my solution to the problem:

    h = {:name => 'foo', :country => 'bar', :age => 22}
    s = "Hello ##name##, you are from ##country## and your age is ##age##. I like ##country##}"
    s.gsub!(/##([a-zA-Z]*)##/) {|not_needed| h[$1.to_sym]}
    

    It generally makes a single pass using regex and does the replacement I think you need.

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

Sidebar

Related Questions

I have a hash like: h = {'name' => 'sayuj', 'age' => 22, 'project'
I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a string which looks like a hash: { :key_a => { :key_1a
In C# Given a hash table like {id:'1', name:'foo'} How can I dynamically create
I have a Ruby hash which looks like: { id => 123, name =>
Suppose I have a ruby hash : person={:name=>:Alex,:age=>10} I wish I could call an
I have a hash that looks like this h1 = {4c09a0da6071a593f051de32=>[4c09a0da6071a593f051de32, Cafe Bistro, 37.78458803130115,
Let's say I have a hash in Ruby like this: d = {1 =>
I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,

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.