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:
“This perceiving, active being is what I call mind, spirit, soul, or myself. By which words I do not denote any one of my ideas, but a thing entirely distinct from them, wherein they exist, or, which is the same thing, whereby they are perceived; for the existence of an idea consists in being perceived.”
—George Berkeley (16851753)
“Cold and hunger seem more friendly to my nature than those methods which men have adopted and advise to ward them off.”
—Henry David Thoreau (18171862)
“Lay down, lay down the bigly bier,
Lat me the dead look on;
Wi cherry cheeks and ruby lips
She lay an smild on him.
O ae sheave o your bread, true-love,
An ae glass o your wine,
For I hae fasted for your sake
These fully day [is] nine.”
—Anna Gordon Brown (17471810)