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:

    Once one is caught up into the material world not one person in ten thousand finds the time to form literary taste, to examine the validity of philosophic concepts for himself, or to form what, for lack of a better phrase, I might call the wise and tragic sense of life.
    F. Scott Fitzgerald (1896–1940)

    I think it is a wise course for laborers to unite to defend their interests.... I think the employer who declines to deal with organized labor and to recognize it as a proper element in the settlement of wage controversies is behind the times.... Of course, when organized labor permits itself to sympathize with violent methods or undue duress, it is not entitled to our sympathy.
    William Howard Taft (1857–1930)

    Its quick silver bell beating, beating
    And down the dark one ruby flare
    Pulsing out red light like an artery,
    Karl Shapiro (b. 1913)