Saturday, September 1, 2007

Improved Auto-Pastie IRB Code

This code makes it possible for you to paste stuff to Pastie directly from IRB without using the mouse. It's a refactoring of code I posted a while ago on my tumblelog (can't find the exact link). The refactoring exposed an additional feature. I was using OS X clipboard code from Projectionist and I realized the Pastie method would be cleaner if I broke out the OS X clipboard stuff into its own object.

The cool part about that is that you now get the object when IRB loads.

This means you can directly do things like

MacClipboard.read

and

MacClipboard.write(stuff)

which of course makes possible the use case where you copy some code you've got in a file, switch over to IRB, and go

eval MacClipboard.read

to see what the code does. And the use case where you write some crazy method to auto-generate image data, and then go

MacClipboard.write(image_data)

before switching into Photoshop.

Actually, I haven't figured out that last one, but it's probably possible. I thought it would be as simple as

MacClipboard.write(File.open("picture.jpg") {|f| f.read})

but in fact that method returns a string. Also, I don't have Photoshop on this computer. Obviously, however, any method which generates a URL can pass that URL to OS X's command-line open command - in fact, that's another tiny new feature. Now when you auto-generate a Pastie from IRB, the method opens that Pastie in a new Safari window.

Anyway, the other neat part of this is that a refactoring exposes new functionality. Technically that's kind of a paradox, since the whole definition of refactoring is changing the design without changing the functionality. Definitely a cool paradox, though.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.