Reputation: 113
I create a file when I run my Ruby script. How would I set the script to open the file with a default editor or a text editor once the script is finished?
For instance, I create a file called "FooBar.txt" that gets a bunch of information loaded into it. How can I open that up afterwards?
I am sure this is really simple, but every time I search for it all it comes up with is opening a file to add text or edit in the script. I'm not actually opening the file with a program.
Upvotes: 1
Views: 940
Reputation: 27855
You can make a system call.
This is an example with Windows:
filename = "the_list.txt"
File.open(filename, "w"){|file|
file << "Some data\n"
}
`call notepad #{filename}`
This calls Notepad with the given filename.
Some variants to call an external program are:
`notepad #{filename}`
system( "notepad #{filename}")
system( "call notepad #{filename}")
%x{call notepad #{filename}}
You even don't need to add notepad:
%x{call #{filename}}
This depend on the main application, which is assigned to the extension of the file you create.
When you tell which system and which editor you need, more details are possible.
Another possibility:
require 'open3'
Open3.popen3("call #{filename}")
#or:
#Open3.popen3("call notepad #{filename}")
The advantage is the main program does not wait until the subprocess ends.
Variant as script: Store the following code as "file_build.rb".
filename = ARGV.first
File.open(filename, "w"){|file|
file << "Some data\n"
}
require 'open3'
puts "Call Editor"
Open3.popen3("call notepad #{filename}")
puts "End of script"
Now you can call file_build.rb test.txt
. test.txt is created, an editor is called and the script closes. The editor keeps running, at least it did in my test (WinXP).
Upvotes: 1