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

My build process consists of Qt's qmake Makefile generator and the typical make utility
I make a distributed embedded application that will make use of several micro-controllers. The
Can Eclipse make parameters for generated methods (overwriting, implementing interface, etc.) final, and if
I want to make some variables I generate in b available in c: a
I've been playing with idea to make a script to generate 2-characters words from
Are there any issues which might make MS's T4 Template code-generation system unsuitable for
Make a new AS3 Document in Flash, paste in the following code and run
make is not only useful for building your programming project, but it seems to
To make it short: hibernate doesn't support projections and query by example? I found
To make things easier when switching between machines (my workstation at the office and

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.