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:
“There are two kinds of people one can call reasonable: those who serve God with all their heart because they know him, and those who seek him with all their heart because they do not know him.”
—Blaise Pascal (16231662)
“Parents ought, through their own behavior and the values by which they live, to provide direction for their children. But they need to rid themselves of the idea that there are surefire methods which, when well applied, will produce certain predictable results. Whatever we do with and for our children ought to flow from our understanding of and our feelings for the particular situation and the relation we wish to exist between us and our child.”
—Bruno Bettelheim (20th century)
“A man in the house is worth two in the street.”
—Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)