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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:36:43+00:00 2026-05-13T11:36:43+00:00

How do you make a generator that alters a file. Im trying to make

  • 0

How do you make a generator that alters a file.

Im trying to make it so that it finds a pattern in a file and adds come content to the line below it.

  • 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-13T11:36:43+00:00Added an answer on May 13, 2026 at 11:36 am

    Rails’ scaffold generator does this when it adds a route to config/routes.rb It does this by calling a very simple method:

    def gsub_file(relative_destination, regexp, *args, &block)
      path = destination_path(relative_destination)
      content = File.read(path).gsub(regexp, *args, &block)
      File.open(path, 'wb') { |file| file.write(content) }
    end
    

    What it’s doing is taking a path/file as the first argument, followed by a regexp pattern, gsub arguments, and the block. This is a protected method that you’ll have to recreate in order to use. I’m not sure if destination_path is something you’ll have access to, so you’ll probably want to pass in the exact path and skip any conversion.

    To use gsub_file, let’s say you want to add tags to your user model. Here’s how you would do it:

    line = "class User < ActiveRecord::Base"
    gsub_file 'app/models/user.rb', /(#{Regexp.escape(line)})/mi do |match|
      "#{match}\n  has_many :tags\n"
    end
    

    You’re finding the specific line in the file, the class opener, and adding your has_many line right underneath.

    Beware though, because this is the most brittle way to add content, which is why routing is one of the only places that uses it. The example above would normally be handled with a mix-in.

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

Sidebar

Related Questions

I'm trying to make a simple generator that takes some syllables from a user
I'm trying to make a truth-table generator for a digital electronics course because that's
I am trying to make a key generator that generates upper case, lower case
I am trying to make a generator function that yields an item on each
To allow caching a PHP generated file, I want to make sure, that the
I'd like to make a number generator that does not repeat the number it
I'm trying to make a reference generator. People fills in some information, and then
I'm trying to make a basic Address class, that many different other classes can
I have a password generator that uses the ascii table. I am trying to
How can I make a terrain generator that makes more realistic terrain? My current

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.