在Ruby脚本中打开文本编辑器 [英] 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)
我目前正在研究一个命令行红宝石,它可以自动执行 https://gist.github.com/jbenet/ee6c9ac48068889b0912 .您可以在 https://github.com/gsmendoza/git_pretty_accept/tree中找到该gem的WIP代码/git_pretty_accept .宝石会做这样的事情:
I'm currently working on a command-line ruby gem that automates the "rebase + no-ff merging" workflow discussed at https://gist.github.com/jbenet/ee6c9ac48068889b0912. You can find the WIP code for this gem at https://github.com/gsmendoza/git_pretty_accept/tree/git_pretty_accept. 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 = File.read('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`
当我尝试通过ruby运行这些git命令时,vi some_temp_file.txt
不会像我希望的那样打开文本编辑器.相反,我看到一条警告"Vim:警告:输出未到终端",脚本只是挂起了.
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.
有什么想法吗?
推荐答案
要在ruby脚本中运行bash命令,可以使用system()
命令:
http://www.ruby-doc. org/core-2.0.0/Kernel.html#method-i-system
To run a bash command from within a ruby script, you can use the system()
command:
http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-system
这将在您的特定情况下产生所需的行为:
This should produce the desired behavior in your particular case:
#!/usr/bin/ruby
system('vim', 'some_temp_file.txt')
从命令行运行该脚本会在Vim中打开给定文件.
Running this script from the command line opens the given file in Vim.
这篇关于在Ruby脚本中打开文本编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!