Suppose you do not want to view the terminal, but instead want the result to appear in TextMate, for example, in the Smalltalk workspace.
Essentially, you want to run ruby ββin a text mat, but you want variables to be remembered between executions. You can have it.
(Thanks to stef for instructions on adding a new command)
- Run
gem install daemons
- Launch the irb server. The file is located below.
- In TextMate, go to Bundles -> Bundle Editor
- Create a new team inside our own package. Name it Run in Terminal
- Set "Save" to "No", set "Input" to the selected text and "or" to "Line".
- Set "Exit to Uninstall"
- In Activation, select your own shortcut. I chose Apple Shift U
- Paste the command below into the "Command" field (formatting is causing me problems).
- Close the Bundle Editor, then select Bundles β Bundle Editor β Reload Bundles
- Create a new document containing the lines
@@hi = "Hello World"
and @@hi + "ya"
- Select the first line you just wrote in Textmate, and press the key combination.
- Do the same with the second line
- Watch how "hiya" appears in a text mat.
Irb server:
#!/usr/bin/env ruby -w require 'rubygems' require 'daemons' require 'socket' LARGE = 100000000 PIPE = "/tmp/.irbservepipe" def kill_pipe `rm -f #{PIPE}` end def respond_to(pipe) inp = pipe.recv LARGE inp.nil? and return begin out = eval(inp) rescue Exception => e out = e end pipe.send out.inspect, 0 end def ensure_server ["EXIT", "INT", "HUP", "TERM"].each {|ea| trap( ea ) { kill_pipe }} File.exists?(PIPE) and kill_pipe server = UNIXServer.new(PIPE) loop { c = server.accept respond_to c c.close } end Daemons.daemonize ensure_server
Command:
#!/usr/bin/env macruby -w require 'socket' LARGE = 100000000 PIPE = "/tmp/.irbservepipe" input = STDIN.read socket = UNIXSocket.new(PIPE) socket.send input, 0 puts socket.recv LARGE
source share