In this series of articles, I will work through the Pragmatic Programmer book Programming Cocoa with Ruby by Brian Marick, porting the examples to MacRuby.

Examples from Chapter 2

The first few examples are almost identical. require 'osx/cocoa' becomes framework 'cocoa'. MacRuby lets us use the familiar SomeObject.new instead of SomeObject.alloc.init, and there is no need to explicitly inherit from NSObject, since NSObject replaces Object in the standard class hierarchy:

irb(main):011:0> class Foo;end;Foo.ancestors
=> [Foo, NSObject, Kernel]
irb(main):012:0>

most-basic-app.rb

#!/usr/bin/env macruby
framework 'cocoa'
NSApplication.sharedApplication
NSApp.run

no-ui.rb

#!/usr/bin/env macruby
framework 'cocoa'

class AppDelegate
  def applicationDidFinishLaunching(aNotification)
    puts "#{aNotification.name} makes me say: Hello, world"
  end
end

our_object = AppDelegate.new
NSApplication.sharedApplication
NSApp.setDelegate(our_object)
NSApp.run

Pages: 1 2 3