[Rubycocoa-devel 542] New APIs: objc_send and objc_export

Back to archive index

Laurent Sansonetti lsans****@apple*****
Wed Jan 3 03:24:28 JST 2007


Hi guys,

As discussed with Hisa-san, I just added 2 new APIs in SVN.

1) objc_send

This is a convenience facility to send messages to an Objective-C  
object in a symbol/value/... like syntax. You may know that using this  
syntax directly on the object (like foo.doSomething(x, :withObject,  
y)) has been deprecated. objc_send is a new method that still let you  
to do that. As it is a separate method it is now less dangerous.

Usage:

NSURL.alloc.objc_send(:initWithScheme, 'http', :host,  
'localhost', :path, '/foo')

2) objc_export

This is again a convenience facility to register Ruby methods to the  
Objective-C runtime. Users may not find addRubyMethod_withType  
convenient as first you need to convert the Ruby method name as an  
Objective-C selector, and 2) you need to pass the Objective-C encoding  
type of the method (everybody do not know the runtime constants).

So, objc_export takes 2 arguments. First is the Ruby name of the  
method, and second is an array that contains the C types.

Usage:

class Foo < OSX::NSObject
   def foo1
     's'
   end
   objc_export :foo1, %w{id}
   # - (id)foo1;

   def foo2(integer)
     integer + 2
   end
   objc_export :foo2, %w{int int}
   # - (int)foo2:(int)integer;

   def foo3_obj(ary, obj)
     ary.addObject(obj)
   end
   objc_export :foo3, %w{void id id}
   # -(void)foo3:(id)ary obj:(id)obj;
end

As you can see objc_export is definitely easier to use than  
addRubyMethod_withType.

Test cases have been added for both objc_send and objc_export. Please  
check them out :)

# I will update doc/unstable for the next release.

Laurent



More information about the Rubycocoa-devel mailing list
Back to archive index