Textmateing a string from irb - a tweak for Gile's new gem, utility_belt

Posted by Tim Connor Wed, 12 Dec 2007 00:37:00 GMT

So Giles Bowkett just released a gem that does most of the tweaks I had been meaning to add to my .irbrc, but had been to lazy. utility_belt is pretty awesome, but there is one tiny tweak I wanted to add to make it optimal, and Giles isn’t known for making himself easy to get a hold of (especially now that comments seem to be perpetually closed on his blog). So instead I just hacked it on the gem myself. I’m not going to bother distributing it as a gem myself, but you are welcome to steal the idea and add it to your copy.

To be able to pass a string (say like the last result, via “_”, when irb just dumped 500 lines of output on you) to Textmate, edit lib/interactive_editor.rb like so


10,13c10,13
< def edit
< unless @file
< @file = Tempfile.new(“irb_tempfile”)
< end
-
> def edit(string)
> @file = Tempfile.new(“irb_tempfile”)
> @file.write string
> @file.close
17a18
> @file.unlink
22c23
< def edit(editor)
-
> def edit(editor, string)
27c28
< IRB.conf[:interactive_editors][editor].edit
-
> IRB.conf[:interactive_editors][editor].edit(string)
30,31c31,32
< def vi
< edit(:vim)
-
> def vi(string=nil)
> edit(:vim,string)
34,35c35,36
< def mate
< edit(:mate)
-
> def mate(string=nil)
> edit(:mate,string)
38,39c39,40
< def emacs
< edit(:emacs)
-
> def emacs(string=nil)
> edit(:emacs,string)

Now glory in “mate _” from irb.

Or best of all: use ruby2ruby and call mate on the result of #to_ruby on a method. You can edit the method in Textmate and save, and wallah, it’s applied. Of course, getting that working correctly in an actual Module/Class hierarchy might be a bit more complicated, and I leave as an exercise for the reader.

Leave a comment

Comments