Ruby console application using text editor?

荒凉一梦 提交于 2019-12-11 17:53:04

问题


I'm building a console application in Ruby. One of the things I'd like it to do is edit text files. It strikes me that the most reasonable course of action would be to launch whatever default editor the user has set up -- nano, vi, emacs... let the user decide. I'd like to pass it the name of the file that will be created/edited.

I've investigated various methods, including the backticks like so:

response = `nano *nameoffile*`

And I've also tried using the POpen4 Ruby gem, which also doesn't seem to do anything.

I suspect these methods are strictly for common, non-shell-type applications that immediately return a result, and editors like nano require a different technique? Your feedback is much appreciated!

Cheers,

Aaron.


回答1:


You might want to take a look at interactive_editor, they do something like that.




回答2:


Also you can use

system "nano #{your_file_name}"



回答3:


There is a good discussion around using exec() or system() or backticks `` to launch interactive processes (like a text-editor)

See it here

It also has info if you want to go deeper into popen4 (which in fact does something, although not suited for all situations), such as the advantages and incovenients



来源:https://stackoverflow.com/questions/4026546/ruby-console-application-using-text-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!