[Rubycocoa-devel 570] Re: rubycocoa 0.9 questions and confusions

Back to archive index

Laurent Sansonetti lsans****@apple*****
Sat Jan 6 02:25:32 JST 2007


On Jan 5, 2007, at 1:22 AM, jeanp****@gmail***** wrote:

> On 1/4/07, Laurent Sansonetti <lsans****@apple*****> wrote:
>
> On Jan 5, 2007, at 12:31 AM, Tim Burks wrote:
> > I've found the same problem, it's related to the trick that is being
> > used to automatically import the class.  When it automatically
> > imports a class in a class declaration, the wrong class is used when
> > the body of the class is parsed.
> >
> > For example, add this "puts" to your code and rerun it (or use
> > OSX.NSLog).
> >
> >>   class OSX::SchoolKid
> >         puts self.superclass
> >>     def saySomething
> >>       puts "hi"
> >>     end
> >>   end
> >
> > You'll probably see that it's "Object" instead of OSX::NSObject.
>
> Ah this is indeed another problem that should also be addressed. But
> the original problem (overriding methods directly in the class) is
> still pertinent, RubyCocoa doesn't allow it yet.
>
> if it would be helpful, i can file it as a bug/ER.
>
> even after i explicitly ns_import :SchoolKid, i wasn't able to  
> override the method and instead got a runtime error:
>   RuntimeError: could not add 'saySomething' to class '': Objective- 
> C cannot find it in the superclass

OK so I spent the past hours fixing this :-)

So bug number one, you can now do "class MyClass" directly and add  
methods there. RubyCocoa will automatically set the right super class  
for you. In fact it was already doing it, but only if you explicitly  
prefixed the class name by OSX. Now you can have the OSX module  
included and it will also work.

Bug number two, if you overrode methods in your Ruby class, RubyCocoa  
will override the Objective-C side for you.

And finally, but number three, you can now add new methods in a given  
Objective-C class from Ruby.

Example!

$ cat test.rb
require 'osx/cocoa'
include OSX

class NSURL
   def host
     'xxx'
   end
end

p NSURL.URLWithString('http://google.com').performSelector('host').to_s

class NSObject
   def mySuperMethod
     'owned!'
   end
   objc_export :mySuperMethod, ['id']
end

p  
NSString.stringWithCString('foo').performSelector('mySuperMethod').to_s

$ ruby test.rb
"xxx"
"owned!"

I committed everything in SVN and added some extra test cases. If you  
want to test this :-)

Cheers,
Laurent



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