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:
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:
Read more about this topic: RubyCocoa
Famous quotes containing the words call, methods and/or ruby:
“Slim/Marie Brown: Whadya think youre gonna do?
Harry Morgan: Im gonna get that wallet, Slim.
Marie: Id rather you wouldnt call me Slim. Im a little too skinny to take it kindly.”
—Jules Furthman (18881960)
“If you want to know the taste of a pear, you must change the pear by eating it yourself.... If you want to know the theory and methods of revolution, you must take part in revolution. All genuine knowledge originates in direct experience.”
—Mao Zedong (18931976)
“Thy great leaves enfold
The ancient beards, the helms of ruby and gold
Of the crowned Magi; and the king whose eyes
Saw the Pierced Hands and Rood of elder rise
In Druid vapour and make the torches dim....”
—William Butler Yeats (18651939)