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.








