[MacRuby-devel] Accessing Obj-C InstVars from Ruby

John Shea johnmacshea at gmail.com
Mon Mar 30 11:27:20 PDT 2009


Hello Edward,

well since no one else has answered i will speculate a bit (it might
provoke someone wiser).

I assume that the Ruby and the Objective C class are magically married
together perhaps via some sort of proxy class - since you cant as far
as i know add to an objective C class more instance methods - and
inserting a compiled class into a ruby class must be also problematic
- anyway it seems complicated. So I assume that the ruby class is
actually still a separate class. I must admit i never tried to extend
a compiled object C class - and I surprised it works as well as it
does.

Given that, what about just adding getters and setters in the usual
way to the objective class manually or with properties (see
http://theocacao.com/document.page/510)?

Or just rewrite the whole class in Ruby?

Cheers,
J





On 3/30/09, Edward Hynes <ehynes at dharmagaia.com> wrote:
> Is there a way to access instance variables defined in an Objective-C
> class from a Ruby extension?  I'm porting some of Apple's example code
> to MacRuby as a learning experience and would like to do so
> incrementally.  Porting the code one class at a time works fine, but
> for some of the larger classes I'd like to port them a few methods at
> a time.  Here's an example of what's happening
>
>
> --- Example.h ---
>
>     @interface Example : NSObject {
>         NSString *instVarA;
>         NSString *instVarB;
>      }
>
>     - (NSString *)methodA;
>     @end
>
>
> --- Example.m ---
>
>     #import "Example.h"
>
>     @implementation Example
>       - (id)init {
>         if (self = [super init]) {
>           instVarA = @"instVarA";
>           instVarB = @"instVarB";
>         }
>         return self;
>       }
>
>       - (NSString *)methodA {
>         return instVarA;
>       }
>
>     @end
>
>
> --- Example.rb ---
>
>     class Example
>       def methodB
>         @instVarB
>       end
>     end
>
>
> Then at runtime, when I execute the following...
>
>     example = Example.alloc.init
>     puts example.methodA.inspect
>     puts example.methodB.inspect
>
>
> .. I get
>
>     "instVarA"
>     nil
>
>
> How do I access InsVarB from Ruby?
>
> Thanks,
> Ed
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>


More information about the MacRuby-devel mailing list