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:
“At length he would call to let us know where he was waiting for us with his canoe, when, on account of the windings of the stream, we did not know where the shore was, but he did not call often enough, forgetting that we were not Indians.... This was not because he was unaccommodating, but a proof of superior manners. Indians like to get along with the least possible communication and ado. He was really paying us a great compliment all the while, thinking that we preferred a hint to a kick.”
—Henry David Thoreau (18171862)
“The methods by which a trade union can alone act, are necessarily destructive; its organization is necessarily tyrannical.”
—Henry George (18391897)
“It is better to be looked over than overlooked.”
—Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)