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

  • Home
  • SEARCH
  • 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 7734963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:22:56+00:00 2026-06-01T07:22:56+00:00

Good afternoon! I am pretty new to Ruby and want to code a basic

  • 0

Good afternoon!

I am pretty new to Ruby and want to code a basic search and replace function in Ruby.
When you call the function, you can pass parameters (search pattern, replacing word).

This works like this: multiedit(pattern1, replacement1, pattern2, replacement2, …)

Now, I want my function to read a text file, search for pattern1 and replace it with replacement2, search for pattern2 and replace it with replacement2 and so on. Finally, the altered text should be written to another text file.

I’ve tried to do this with a until loop, but all I get is that only the very first pattern is replaced while all the following patterns are ignored (in this example, only apple is replaced with fruit). I think the problem is that I always reread the original unaltered text? But I can’t figure out a solution. Can you help me? Calling the function the way I am doing it is important for me.

def multiedit(*_patterns)

  return puts "Number of search patterns does not match number of replacement strings!" if (_patterns.length % 2 > 0)

  f = File.open("1.txt", "r")  
  g = File.open("2.txt", "w")

  i = 0

  until i >= _patterns.length do
    f.each_line {|line|
      output = line.sub(_patterns[i], _patterns[i+1])
      g.puts output
    }
    i+=2  
  end

  f.close
  g.close

end

multiedit("apple", "fruit", "tomato", "veggie", "steak", "meat")

Can you help me out?

Thank you very much in advance!

Regards

  • 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-01T07:22:58+00:00Added an answer on June 1, 2026 at 7:22 am

    Your loop was kind of inside-out … do this instead …

      f.each_line do |line| 
        _patterns.each_slice 2 do |a, b|
          line.sub! a, b
        end
        g.puts line
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Afternoon Gurus, I am pretty familiar with basic socket programming, and the IO::Socket
Good afternoon, I am currently in the very early phase of a new project
Good afternoon, I'm having a problem with a section of code I'm working on
Good afternoon all, I am building a function that takes a string as input,
Good afternoon all, I was taught that when a function returns, The variables (within
Good afternoon, I need to be able to search a Lucene.Net index with a
Good Afternoon Developer, this is my content in asp .net, I want the user
Very good afternoon to all, The problem I have now is that I can
Good afternoon - I have a pretty general question today - I've been tasked
Good afternoon Overflowers! ;) What I want to do: I'm interested in verifying transferred

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.