Suppose you don't want to look over at the Terminal, but instead want the result to show up in TextMate, like in a Smalltalk workspace.
In essence, you want to run ruby within text mate, but you want variables to be remembered between executions. You can have that.
(Thanks stef for instructions on how to add a new command)
- Run
gem install daemons
- Run an irb server. The file is below.
- In TextMate go to Bundles -> Bundle Editor
- Create a new Command inside our own Bundle. Call it "Execute in Terminal"
- Set "Save" to Nothing, set "Input" to Selected Text and "or" to Line.
- Set "Output to Discard
- In "Activation" choose your own shortcut. I chose Apple Shift U
- Paste the command below into the "Command" box (formatting is causing me trouble)
- Close the Bundle Editor and then choose Bundles -> Bundle Editor -> Reload Bundles
- Create a new document containing the lines
@@hi = "Hello World"
and @@hi + "ya"
- Select the first line you have just written in Textmate and press your keyboard shortcut.
- Do the same with the second line
- Watch as "hiya" appears in text mate.
The 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
The 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