andymatuschak.org: Square Signals

This article was published on Thursday, November 29th, 2007 at 9:37 pm.

Animals ringtonesToby Keith ringtones

RubyCocoa + IRB for Testing!

Sometimes, I find myself wondering how a Cocoa method will behave if I do a certain thing. Can I pass nil for this argument? What keys does this return?

Often, I’ll build little test apps to solve these kinds of problems. That requires make a new Xcode project, which has its own directory, then, in many cases, adding some kind of class to instantiate in IB, then calling some methods when applicationDidFinishLaunching:.

Way too much work.

Now, next time I’m wondering which formats NSImage supports, I can just open up IRB in the Terminal and type things like:

require 'osx/cocoa'
OSX::NSImage.imageFileTypes

Well, that was my first try, anyway. That just spits out:

#<OSX::NSCFArray:0x2e09dc class='NSCFArray' id=0x1a73040>

Now, if we passed this NSArray to any Ruby method requiring a Ruby array, it’d be converted automatically:

OSX::NSImage.imageFileTypes.collect { |type| type.reverse }

But if we want to just convert it, we can use the handy to_ruby method:

>> OSX::NSImage.imageFileTypes.to_ruby
=> ["'PDF '", "PDF", "pdf", "'PICT'", "PIC", "pic", "PCT", "pct",
"PICT", "pict", "'EPSF'", "PS", "ps", "EPSI", "epsi", "EPSF", "epsf",
"EPI", "epi", "EPS", "eps", "XBM", "xbm", "HDR", "hdr", "EXR", "exr",
"CR2", "cr2", "DCR", "dcr", "DNG", "dng", "SRF", "srf", "NEF", "nef",
"CRW", "crw", "RAF", "raf", "ORF", "orf", "MRW", "mrw", "ICNS",
"icns", "'jp2 '", "JP2", "jp2", "'qtif'", "QTI", "qti", "QTIF",
"qtif", "'TPIC'", "TGA", "tga", "TARGA", "targa", "'.SGI'", "RGB",
"rgb", "SGI", "sgi", "'8BPS'", "PSD", "psd", "'PNTG'", "MAC", "mac",
"PNT", "pnt", "PNTG", "pntg", "'FPix'", "FPIX", "fpix", "FPX", "fpx",
"'PNGf'", "PNG", "png", "'GIFf'", "GIF", "gif", "'JPEG'", "JPG", "jpg",
"JPEG", "jpeg", "CUR", "cur", "'ICO '", "ICO", "ico", "'BMPf'", "BMP",
"bmp", "'TIFF'", "FAX", "fax", "TIF", "tif", "TIFF", "tiff"]

This technique is really useful for spiking things out without having to make a whole new project.

The Conversation {7 comments}

  1. Henrik N 29 November, 07 @ 11:59 pm

    Seems like it could be a nice idea to write a tiny wrapper script (”irc”?) that opens IRB with that require + perhaps an include OSX.

  2. Giuseppe Alletti 30 November, 07 @ 4:26 am

    I often find very useful also Mark Dalrymple’s tecnique:
    http://borkwarellc.wordpress.com/2007/08/25/in-praise-of-little-command-line-apps/

  3. Laurent Sansonetti 30 November, 07 @ 6:01 am

    RubyCocoa ships with an example called CocoaRepl, which is an interactive Ruby window, where you can enter expressions and evaluate them on the fly. It’s often used for testing.

    Also note that RubyCocoa 0.13.0 (the latest release) provides custom inspect methods for many Cocoa classes. NSArray#inspect for example will display all the elements, which is very nice for testing too.

  4. Andy Matuschak 30 November, 07 @ 4:28 pm

    Laurent: I tried OSX::NSImage.imageFileTypes.inspect, but it just returned "#<OSX::NSCFArray:0x3035a4 class='NSCFArray' id=0x1a2b550>"

  5. Laurent Sansonetti 02 December, 07 @ 4:42 pm

    Works for me. Note that you need 0.13.0.

    $ irb -r osx/cocoa
    >> OSX::NSImage.imageFileTypes.inspect
    => “#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #]>”
    >> OSX::RUBYCOCOA_VERSION
    => “0.13.0″
    >>

  6. Laurent Sansonetti 02 December, 07 @ 4:43 pm

    Oops, sorry for the bad markup… here is a pastie instead: http://pastebin.com/m484ae716

  7. Andy Matuschak 03 December, 07 @ 5:24 pm

    Oh ho! I didn’t realize that the Leopard-bundled version wasn’t the newest one. Thanks, Laurent.

Leave a Comment

Currently you have JavaScript disabled. In order to post comments, please make sure JavaScript and Cookies are enabled, and reload the page.

You can follow any responses to this entry via its RSS comments feed. You can also leave a trackback if the inclination is there.

If you're looking for something specific then give the search form below a try:

RSS Wordpress Grady (theme) Return to the Top ↑