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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:27:27+00:00 2026-05-23T01:27:27+00:00

I wanted to make a generator that created files (and directories, etc…) based on

  • 0

I wanted to make a generator that created files (and directories, etc…) based on already existing files in the app (for instance, the views or controllers). So if we had views set up like this

-app
   -views 
        - layouts
             - application.html.erb
        - users
             - index.html.erb
             - show.html.erb 
             - etc ...

and I wanted to create files based on them I can do (with just ruby)

directories = Dir.entries("#{Rails.root}/app/views")
directories.each do |directory|
  unless directory == "." or directory == ".."
    files = Dir.entries("#{Rails.root}/app/views/#{directory}")
    files.each do |file|
      unless file == "." or file == ".."
        text = File.read("#{Rails.root}/app/views/#{directory}/#{file}")      
        something #=> whatever else needs to go here to edit the file
        something else #=> output_file.puts whatever
      end
    end
  end
end

so this is basically what I would like to do with a generator so I can roll my code into a plugin and use it for other apps.

First question, how can I generate arbitrary files (with filenames based on existing filenames using the generator. Is it appropriate to cycle through the directories like I did above, grab the directory/file and generate files? Is there a way to do what I did using a simpler method (mine seems easily breakable).

Also, should I put all that read/format/write code inside the generator itself and just pass a string into the "initialize content" section of create_file or should I put it somewhere else. Or should I use the generator to create the bare files and populate it with an init script?

Is there a more rails type of way of populating generated files, or should I just shove all my formatting code inside the generator. If so, what is the appropriate way to approach this.

  • 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-23T01:27:28+00:00Added an answer on May 23, 2026 at 1:27 am

    I am not sure if you want to know how generators are built in rails3 or not. The code you are showing is not very generator-like. In generators you can use all commands from Thor, which offers you a very powerful toolset of manipulating files, and injecting code (strings) into classes or files.

    So I would most definitely fill your files inside a generator, because then it happens on user request, and the user can choose whether or not certain files need or can be overwritten or not.

    Inside your gem, you will have a lib/generators folder, containing a templates folder, containing all files you might want to place inside the rails application.

    From the Thor documentation, here is a nice example to construct files in a generator.

    Hope this helps.

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

Sidebar

Related Questions

In a program that I'm writing, I wanted to make a ConfigParser that's read
I wanted to make a special version of shared_ptr that would perform specific operations
I wanted to make Map of Collections in Java, so I can make something
Let's say I wanted to make a python script interface with a site like
This might be a stupid question but I just wanted to make sure... If
before posting the whole code, i wanted to make sure I am not missing
I have been bitten by something unexpected recently. I wanted to make something like
PERL? Perl? perl? What's good style? I know the answer—I just wanted to make
I've always wanted a way to make a socket connection to a server and
I'm about to create some complex Ant build files and I wanted find out

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.