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:
“Yet things are knowable! They are knowable, because, being from one, things correspond. There is a scale: and the correspondence of heaven to earth, of matter to mind, of the part to the whole, is our guide. As there is a science of stars, called astronomy; and science of quantities, called mathematics; a science of qualities, called chemistry; so there is a science of sciences,I call it Dialectic,which is the Intellect discriminating the false and the true.”
—Ralph Waldo Emerson (18031882)
“Generalization, especially risky generalization, is one of the chief methods by which knowledge proceeds... Safe generalizations are usually rather boring. Delete that usually rather. Safe generalizations are quite boring.”
—Joseph Epstein (b. 1937)
“A man in the house is worth two in the street.”
—Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)