Examining RubyCocoa
by Chris Thomas


Example 1:
(a)
array = [[NSMutableArray alloc] init];

(b)
array = OSX::NSMutableArray.alloc.init

(c)
[array release];


Example 2:

(a)
[dictionary setObject:object forKey:key];

(b)
dictionary.setObject_forKey_( object, key )

(c)
dictionary.setObject_forKey( object, key )

(d)
dictionary.setObject( object, :forKey, key )

Example 3:
(a)
string.drawAtPoint( NSPoint.new(0,0) )
string.stringWithRange( NSRange.new(0,20) )

(b)
string.drawAtPoint( [0,0] )

(c)
string.stringWithRange( 0..19 )

Listing One
require 'osx/cocoa'
class HelloView < OSX::NSView
    # Tell RubyCocoa to setup Objective-C overrides
    # for the NSView method drawRect:.
    ns_overrides    :drawRect_
        # When the Cocoa view system wants to draw a view, it calls the 
        # method -(void)drawRect:(NSRect)rect. The rectangle argument is 
        # relative to the origin of the view's frame, and it may only be 
        # a small portion of the view. For this reason, simple views with
        # only one or two graphical elements tend to ignore this parameter.
    def drawRect(rect)
        # Set the window background to transparent
        OSX::NSColor.clearColor.set
        OSX::NSRectFill(bounds)
        # Draw the text in a shade of red and in a large system font
        attributes = OSX::NSMutableDictionary.alloc.init
        attributes.setObject_forKey(  OSX::NSColor.redColor, 
                                      OSX.NSForegroundColorAttributeName )
        attributes.setObject_forKey(    
                                      OSX::NSFont.boldSystemFontOfSize(48.0),
                                      OSX.NSFontAttributeName )
        string = OSX::NSString.alloc.initWithString( 
                                      "Hello, Ruby Baby" )
        string.drawAtPoint_withAttributes([0,0], attributes)
        # Turn window's shadow off/on. This is a kludge to get the shadow
        # to recalculate for the new shape of the opaque window content.
        viewWindow = window
        window.setHasShadow(0)
        window.setHasShadow(1)
    end
end
# If this file is the main file, then perform the following commands. (This
# construct is often useful for adding simple unit tests to library code.)
if __FILE__ == $0
    # First, to establish a connection to the window server,
    # we must initialize the application
    application = OSX::NSApplication.sharedApplication

    # Create the window
    window = OSX::NSWindow.alloc.initWithContentRect([0, 0, 450, 200],
                :styleMask, OSX::NSBorderlessWindowMask,
                :backing,   OSX::NSBackingStoreBuffered,
                :defer,     0)
    # Allow the window to be partially transparent
    window.setOpaque(0)

    # Setup the window's root view
    view = HelloView.alloc.initWithFrame([0, 0, 450, 200])
    window.setContentView(view)

    # Place the window near the top of the screen.
    # (Screen coordinates in Cocoa are always PostScript coordinates, which 
    # start from the bottom of the screen and increase as they go up, so we 
    # have to do some math to place the window at 100 pixels from the top 
    # of the screen.
    screenFrame = OSX::NSScreen.mainScreen.frame
    windowOriginPoint = 
               [40, screenFrame.origin.y + screenFrame.size.height - 100]
               window.setFrameOrigin( windowOriginPoint )
    # Show the window
    window.makeKeyAndOrderFront(nil)

    # And start the application event loop
    application.run
end

Listing Two
#import <RubyCocoa/RBRuntime.h>
int main( int argc, char* argv[] )
{
  return RBApplicationMain("helloruby.rb", argc, argv);
}







1

