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}
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.I often find very useful also Mark Dalrymple’s tecnique:
http://borkwarellc.wordpress.com/2007/08/25/in-praise-of-little-command-line-apps/
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.
Laurent: I tried
OSX::NSImage.imageFileTypes.inspect, but it just returned"#<OSX::NSCFArray:0x3035a4 class='NSCFArray' id=0x1a2b550>"Works for me. Note that you need 0.13.0.
$ irb -r osx/cocoa
>> OSX::NSImage.imageFileTypes.inspect
=> “#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #]>”
>> OSX::RUBYCOCOA_VERSION
=> “0.13.0″
>>
Oops, sorry for the bad markup… here is a pastie instead: http://pastebin.com/m484ae716
Oh ho! I didn’t realize that the Leopard-bundled version wasn’t the newest one. Thanks, Laurent.
Leave a Comment
You can follow any responses to this entry via its RSS comments feed. You can also leave a trackback if the inclination is there.