Ruby - Write output to new terminal window

652 views Asked by At

I was wondering whether it is possible to have a ruby script open a new terminal window and redirect its output to that newly-opened terminal window.

To open a new terminal, I currently do system("gnome-terminal"), however, after researching for a while, I still dont know how I could achieve the following ("pseudo code"):

variable = "test"
newTerminal = system("gnome-terminal")
puts variable to newTerminal

Is that even possible without going through major troubles?

2

There are 2 answers

0
chills42 On

I can't easily test this for your system, but I'd try using IO.popen to open the terminal, which should allow you to write to the IO stream returned.

0
raph On

To do something similar on OSX (if ever someone runs into the same situation, yes I know this doesn't answer it for this specific situation) you can do:

variable = "test"
`osascript -e 'tell app "Terminal"
  do script "echo #{variable}"
end tell'`

Note: This isn't exactly the same as using puts since it would actually execute the echo command multiple times if you wanted to print several things but its more of a foundation for how to solve a similar issue.