[MacRuby-devel] Mixing Objective-C and Ruby classes

John Shea johnmacshea at gmail.com
Fri Apr 17 02:15:05 PDT 2009


Hello Victor,

I see that there is not much action on the list at the moment - so I  
will reply - the heavyweights do eventually drop by - but they are  
very busy so it is a bit sporadic (unless its just my gmail playing up  
again).

I haven't actually tried 2 and 3 on your list since I am trying to  
obviate the need to actually write anything in Objective C. I guess if  
you have Obj C code already then you might want to call ruby rather  
than the other way around, so I will leave those questions for others  
and answer the simple one.

For number 1:
If you are using Xcode it all seems to work magically, and I am not  
sure why you are having problems.

If I want to call some Obj C code from a ruby class - i first create  
an obj c class (eg MyClass) the normal way in Xcode (with .m and .h  
classes becoming part of the project), write the obj c code, and then  
any ruby code can magically use that class - eg with:

MyClass.my_class_method

or

a = MyClass.new
a.my_instance_method

or
a =MyClass.init.alloc
a.my_instance_method

if you want to access state in the obj c class - there was a thread on  
that recently (subject Accessing Obj-C InstVars from Ruby) - but I use  
getters and setters in the Obj C code.

Hope that helps,
John


On Apr 16, 2009, at 13:23 , macruby-devel at principia.info wrote:

> Hi,
>
>  I'm new both to this list and to MacRuby. Let this message serve as  
> an introduction.
>
>  I have some questions that have not seen answered either in the  
> docs or in the list archives. I have been known to miss things  
> before, so please kindly point  me in the right direction if this is  
> documented somewhere.
>
> I have seen the embedding sample but I stil can't figure out how I  
> can pull out the following:
>
> 1. Mix Obj-C and Ruby Classes: ie, from Ruby be able to extend or  
> use Obj-C classes that are lying around in my project. How do I  
> "require" them? When I try to use them, ruby doesn't seem to  
> recognize them (specifically, it recognizes the classes but not  
> their methods)
> 2. The same question, but the other way around: how can I use or  
> extend a class declared in Ruby from Obj-C? The only thing that I  
> can come up with is to declare them with @class, but it didn't work  
> (or I didn't do it properly)
> 3. If I were to call arbitrary scripts as in the embedding sample,  
> how can I pre-initialize ruby objects so that they are available in  
> the environment? I.e, if I allow the user to extend my app by  
> running their ruby code, how can I make my objects available to him?  
> And also, how could I prevent certain objects from being available  
> to him, so that he doesn't break anything?
>
>
> TIA,
>
> victor _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090417/6e68ae4c/attachment.html>


More information about the MacRuby-devel mailing list