gsmendoza gsmendoza - 1 year ago 81
Bash Question

Open text editor in a ruby script

(Disclaimer: I posted a similar question earlier: In a ruby script, how to ask git to open its message editor. I decided to post this as a separate question because I think this question is more generic and thus possibly more applicable to other programmers. However, I kept the git-related question because I'm not sure if an answer here may apply there too. If there are rules against this, let me know)

I'm currently working on a command-line ruby gem that automates the "rebase + no-ff merging" workflow discussed at You can find the WIP code for this gem at The gem would do something like this:

`vi some_temp_file.txt`
`git co master`
`git pull`
`git co pull_request`
`git rebase master`
`git co master`

merge_message ='some_temp_file.txt')
`git merge --message "#{merge_message}" --no-ff pull_request`

`git push`
`git branch -d pull_request`
`git push origin:pull_request`

When I try to run these git commands via ruby,
vi some_temp_file.txt
doesn't open the text editor like I hope it would. Instead, I see a warning "Vim: Warning: Output is not to a terminal" and the script just kind of hangs.

Any ideas?

Answer Source

To run a bash command from within a ruby script, you can use the system() command:

This should produce the desired behavior in your particular case:

system('vi', 'some_temp_file.txt')

Running this script from the command line opens the given file in Vim.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download