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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:34:24+00:00 2026-05-30T17:34:24+00:00

i am a ruby beginner and i found a problem, i would like to

  • 0

i am a ruby beginner and i found a problem, i would like to know if there is a more ‘ruby way’
to solve it.

my problem is:
i got a string, like this:

 str = "<div class=\"yui-u first\">\r\n\t\t\t\t\t<h1>Jonathan Doe</h1>\r\n
 \t\t\t\t\t<h2>Web   Designer, Director</h2>\r\n\t\t\t\t</div>"

 # now, i want to replace the substring in <h1> </h1> and <h2> and </h2> with 
 these two string:"fooo" and "barr".

here is what i did:

# first, i got the exactly matched substrings of str:
r = str.scan(/(?<=<h\d>).*?(?=<\/h\d>)/)
# then, i create a hash table to set the corresponding replace strings
h = {r[0] => 'fooo', r[1] => 'barr'}
# finally, using str.gsub to replace those matched strings
str.gsub!(/(?<=<h\d>).*?(?=<\/h\d>)/, h)
# or like this
str.gsub!(/(?<=<h\d>).*?(?=<\/h\d>)/) {|v| h[v]}

PS: The substring in <h1> </h1> and <h2> </h2> are not fixed, so i have
to get these strings FIRST, so that i can build a hash table. But I
really don’t like the code above (because i wrote two lines almost the same),
i think there must be a elegant way to do so. i have tried something like this:

str.gsub!(/(?<=<h\d>).*?(?=<\/h\d>)/) { ['fooo', 'barr'].each {|v| v}}

but this didn’t work. because this block returns [‘fooo’, ‘barr’] EVERYTIME!
if there is a way to let this block (or something?) return one element at a time(return ‘fooo’ at the first time, then return ‘barr’ at the second), my problem will be solved!
thank you!

  • 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-30T17:34:26+00:00Added an answer on May 30, 2026 at 5:34 pm

    Although you really have no business parsing HTML with a regexp, as a library like Nokogiri can make this significantly easier as you can modify the DOM directly, the mistake you’re making is in presuming that the iterator will execute only once per substitution and that the block will return only one value. each will actually return the object being iterated.

    Here’s a way to avoid all the Regexp insanity:

    require 'rubygems'
    gem 'nokogiri'
    require 'nokogiri'
    
    str = "<div class=\"yui-u first\">\r\n\t\t\t\t\t<h1>Jonathan Doe</h1>\r\n
     \t\t\t\t\t<h2>Web   Designer, Director</h2>\r\n\t\t\t\t</div>"
    
    html = Nokogiri::HTML(str)
    
    h1 = html.at_css('h1')
    h1.content = 'foo'
    
    h2 = html.at_css('h2')
    h2.content = 'bar'
    
    puts html.to_s
    

    If you want to do multiple substitutions where each gets a different value, the simple way is to just rip off values from a stack:

    subs = %w[ foo bar baz ]
    
    string = "x x x"
    
    string.gsub!(/x/) do |s|
      subs.shift
    end
    
    puts string.inspect
    # => "foo bar baz"
    

    Keep in mind that subs is consumed here. A more efficient approach would be to increment some kind of index variable and use that value instead, but this is a trivial modification.

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

Sidebar

Related Questions

I'm a beginner of Ruby and Rails, so this is probably an easy question.
I'm a beginner at Ruby on Rails so I apologize if this is quite
I know nothing about ruby. Do you know some beginner video tutorials for ruby?
I am a Ruby beginner. From the book, I know that a Ruby method
I'm really just a beginner to ruby, so hopefully this is an easy one.
I am beginner in Ruby and having trouble understanding this code require_relative 'custom_page' module
I am a beginner at Ruby on Rails. Currently, I have the following problem:
I'm a beginner in ruby and in programming as well and need help with
I am a beginner to ruby on rails.But i am quite comfortable with php,
Ruby's standard popen3 module does not work on Windows. Is there a maintained replacement

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.