Using Ruby-Cocoa - how to send a Obj-C object a msg?

Hello,

I’m using Ruby-Cocoa and I want to be able to include some of my own classes
that I’ve written in Obj-C and create instances of them in Ruby and then
send messages.

I’ve looked in the /usr/lib/ruby/osx directory and found an oc_wrapper.rb
file that uses code something like the following:

mystring = OSX::NSString.stringWithString(“Hello”)
strLength = self.ocm_send(:length)
puts strLength

Does anyone know exactly what modules/classes in need to require and the
correct way to use this to be able to access my Obj-C classes?

Thanks for your time and help,

···


Sam Griffith Jr.
email: staypufd@mac.com
Web site: http://homepage.mac.com/staypufd/index.html

I figured out what I did wrong on the code below… I was sending to self,
and should have been sending to the local mystring.

require ‘osx/cocoa’

mystring = OSX::NSString.stringWithString(“Hello”)
strLength = mystring.ocm_send(:length)
puts strLength

But part of the question still stands! How do I tell Ruby-Cocoa about a new
Obj-C class that I’ve created and want to use from Ruby?

Thanks for the time,

···


Sam Griffith Jr.
email: staypufd@mac.com
Web site: http://homepage.mac.com/staypufd/index.html

On 5/14/2003 1:40 PM, in article BAE7F7A2.1AEC1%staypufd@austin.rr.com, “Sam Griffith” staypufd@austin.rr.com wrote:

Hello,

I’m using Ruby-Cocoa and I want to be able to include some of my own classes
that I’ve written in Obj-C and create instances of them in Ruby and then
send messages.

I’ve looked in the /usr/lib/ruby/osx directory and found an oc_wrapper.rb
file that uses code something like the following:

mystring = OSX::NSString.stringWithString(“Hello”)
strLength = self.ocm_send(:length)
puts strLength

Does anyone know exactly what modules/classes in need to require and the
correct way to use this to be able to access my Obj-C classes?

Thanks for your time and help,

I found out myself… But for those of you interested, here is more detail:

Notes on using Ruby-Cocoa

To use an Obj-C class that I’ve created, you can use the following code:

OSX.ns_import(aSymbol) takes a symbol of the class name that you want to
include into the OSX namespace.

OSX.ns_import(‘ObjcController’)
puts OSX::ObjcController.alloc.init.getAnInt

To use lower level versions of the method sending, you can use the
.ocm_send() form like in the example below. Note, with being able to do
the above, the need for this is very rare.

#!/usr/bin/ruby

#require ‘osx_objc/’
require ‘osx/cocoa’

mystring = OSX::NSString.stringWithString(“Hello”)
strLength = mystring.ocm_send(:length)
puts strLength

Hope someone else using Ruby-Cocoa finds this useful…

···


Sam Griffith Jr.
email: staypufd@mac.com
Web site: http://homepage.mac.com/staypufd/index.html

On 5/14/2003 1:50 PM, in article BAE7FA06.1AEC6%staypufd@austin.rr.com, “Sam Griffith” staypufd@austin.rr.com wrote:

I figured out what I did wrong on the code below… I was sending to self,
and should have been sending to the local mystring.

require ‘osx/cocoa’

mystring = OSX::NSString.stringWithString(“Hello”)
strLength = mystring.ocm_send(:length)
puts strLength

But part of the question still stands! How do I tell Ruby-Cocoa about a new
Obj-C class that I’ve created and want to use from Ruby?

Thanks for the time,

Use a module function OSX::ns_import to import an Objective-C
class which was already loaded on Objective-C world.

OSX.ns_import :YourObjcClass
# Here, you can use YourObjcClass on Ruby world.
obj = OSX::YourObjcClass.alloc.init

Incidentally, when YourObjcClass exist on the other framework, and
you want to load the framework. In such a case you may use
NSBundle to load it. For example appear in
RubyCocoa.framework/Resources/ruby/osx/objc/addressbook.rb:

load AddressBook.framework

NSBundle.bundleWithPath(“/System/Library/Frameworks/AddressBook.framework”).load

regards,

···

At Thu, 15 May 2003 04:05:49 +0900, Sam Griffith wrote:

But part of the question still stands! How do I tell Ruby-Cocoa about a new
Obj-C class that I’ve created and want to use from Ruby?


FUJIMOTO Hisakuni