RubyCocoa - How To Call Objective-C Methods From Ruby

How To Call Objective-C Methods From Ruby

To invoke an Objective-C method, you replace each colon in the method name except the last with an underscore. Thus, for example, the NSWindow instance method initWithContentRect:styleMask:backing:defer: becomes initWithContentRect_styleMask_backing_defer.

All Cocoa classes and functions belong to OSX module, so for example, the Objective-C code:

initWithContentRect:frame styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]

will become:

OSX::NSWindow.alloc.initWithContentRect_styleMask_backing_defer(frame, OSX::NSTitledWindowMask, OSX::NSBackingStoreBuffered, false)

As you can see, this decreases the code readability by rendering Objective-C parameter naming useless. So, there is another convenient way to write the method calls — the objc_send method, which accepts Ruby symbols as parameter names. For example, the previous code can also be written as:

OSX::NSWindow.alloc.objc_send(:initWithContentRect, frame, :styleMask, OSX::NSTitledWindowMask, :backing, OSX::NSBackingStoreBuffered, :defer, false)

Read more about this topic:  RubyCocoa

Famous quotes containing the words call, methods and/or ruby:

    You don’t know what you might be if you would look beyond the ball, the opera, the fashion-plate—and right over the heads of the perfumed, mustached bipeds who call themselves men and worship at your feet.
    Mattie Chappelle, U.S. women’s magazine contributor. The Revolution (April 28, 1870)

    All men are equally proud. The only difference is that not all take the same methods of showing it.
    François, Duc De La Rochefoucauld (1613–1680)

    A man in the house is worth two in the street.
    Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)