Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open text editor in a ruby script

Tags:

shell

io

ruby

(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 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`

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?

like image 389
gsmendoza Avatar asked Nov 09 '13 07:11

gsmendoza


People also ask

How do I edit a .RB file?

Because RB files are XML files, you can open and edit them in any text editor, including: Microsoft Notepad (Windows) Apple TextEdit (Mac)

Which is an editor used to write your Ruby programs?

Atom Editor (Highly configurable text editor/Free Ruby IDE) An open-source Ruby IDE with incredibly customizable features to make code writing easier is the Atom editor from Github, now a Microsoft company.

How do I run a command-line in Ruby?

Press Ctrl twice to invoke the Run Anything popup. Type the ruby script. rb command and press Enter . If necessary, you can specify the required command-line options and script arguments.


1 Answers

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')

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

like image 144
Jake Romer Avatar answered Dec 07 '22 10:12

Jake Romer