1

I am new to Ruby, and I am trying to figure out a few things. My current problem is this: What is the easiest way to enter a directory (given as a command line argument), run every .rb file in the folder, and gather the results. The program should write every result in a format like "filename :: OK" or "filename :: WRONG" depending on the result.

I use system ("ruby filename.rb") to run the files, and Dir.chdir("Folder") to get in the folder, but I have no idea how to iterate through them, and work them out. Any suggestions?

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
Duplakk
  • 11
  • 2
  • Take a look at http://stackoverflow.com/questions/1755665/get-names-of-all-files-from-a-folder-with-ruby to get a list of the files in a directory. Does that help? – bbill Jun 05 '15 at 18:08
  • 3
    Must be a homework assignment or something as this has been asked recently: http://stackoverflow.com/questions/30631576/how-can-i-scan-a-directory-get-the-ruby-files-execute-them-and-get-the-output – pjd Jun 05 '15 at 18:19
  • @pjd yeah i answered this once and saw 2 other questions like it already – locoboy Jun 05 '15 at 18:20
  • @locoboy If you can find those other questions, could you link them here so we can close them as duplicates? – Ajedi32 Jun 05 '15 at 18:22
  • @Ajedi32this is a duplicate of the question that pjd linked to. – locoboy Jun 05 '15 at 18:24
  • @locoboy Yeah, I was going to vote to close this as a dup of that one, but SO won't let me. (No accepted or upvoted answer on the other question, and I'm not sure I want to upvote the one that's there.) You mentioned there being another question though besides the one that's linked? – Ajedi32 Jun 05 '15 at 18:25

1 Answers1

1

It's as simple as

for file in Dir['./*.rb']
    system('ruby ' + file)
    # return status stored in $? variable
    # files will still output to commandline normally
end

Don't recommend running that file as it is ;) Might have infinite loops or something.

bbill
  • 2,264
  • 1
  • 22
  • 28
  • Yeah, I realized it would be best to find my own method, so I stopped looking for help, and finished the task in a relatively short time, so I guess I can close this topic, since there are similar ones. Thanks for the answers guys – Duplakk Jun 05 '15 at 20:11
  • You'll learn, don't let StackExchange get you down :) Google never judges. – bbill Jun 05 '15 at 20:58