utility_belt tweak 2 - passing a string directly to pastie

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

Instead of having to run it through the clipboard first:

def pastie(string=nil)
  pastie_url = Net::HTTP.post_form(URI.parse("http://pastie.caboo.se/pastes/create"),
                                   {"paste_parser" => "ruby",
                                    "paste[authorization]" => "burger",
                                    "paste[body]" => (string || MacClipboard.read)}).body.match(/href="([^\"]+)"/)[1]
  MacClipboard.write(pastie_url)
  system("open #{pastie_url}")
  pastie_url
end

If you are wondering WTF is going on, read the previous post

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.