RubyCocoa question


#1

Hi all,

I’m trying to build a simple Cocoa app using RubyCocoa.

I’ve no experience with Cocoa, but I’m not trying to make anything
fancy. In short, I’d like to create a window that acts as an observer
for my terminal app. It’ll give feedback to the user with a progress
bar and a few more things.

Now the problem is that app.run never returns:

app = NSApplication.sharedApplication
app.setDelegate(AppDelegate.alloc.init)
app.run

I now I cannot use Ruby threads within RubyCocoa code, and I’m stuck
using Cocoa worker threads. I’ve done this, but it doesn’t work:

class AppRunner < NSObject
def run
pool = NSAutoreleasePool.alloc.init
app = NSApplication.sharedApplication
app.setDelegate(AppDelegate.alloc.init)
app.run
pool.release
end
end

runner = AppRunner.alloc.init
NSThread.detachNewThreadSelector_toTarget_withObject(:run, runner,
nil)

It runs but the app doesn’t pop up. I suspect it is due to the usage
of a worker thread. But my knowledge of Cocoa is non-existent.

Any ideas?

I know MacRuby could be an option, but my project is not 1.9
compatible due to some gem dependencies.

Thanks in advance.


#2

haven’t used rubycocoa, but I’ll try…:wink:

On Apr 4, 2009, at 12:55 PM, abc wrote:

app = NSApplication.sharedApplication

that looks ok

app.setDelegate(AppDelegate.alloc.init)

this doesn’t look right; you’d typically want one of your classes to
act as the NSApplicationDelegate object. Is AppDelegate one of your
classes?

app.run

I now I cannot use Ruby threads within RubyCocoa code, and I’m stuck
using Cocoa worker threads. I’ve done this, but it doesn’t work:

class AppRunner < NSObject
def run
pool = NSAutoreleasePool.alloc.init

I wouldn’t think you’d have to alloc and init the NSAuto… class,
typically any object you’d want auto released you could just call
autorelease on, and you’re not making any other calls involving the
pool var before you release it at the end of your method def, so why
is it there?

nil)

No idea here, but is it necessary to run the app in a separate thread?


#3

Thanks.

Yes, it is necessary to run the app in a separate thread. I have a big
text app and the Cocoa window is intended to be registered as an
observer to that app. There are other non-graphical observers too.
Inverting this would change the design a lot.

The thing is that as app.run never returns (which is ok) I need to run
the app in another thread (or process, but that would make things more
complex).

As to the above code, AppDelegate is one of my classes (the one that
creates the window). The rest is something I took from a RubyCocoa
doc, but I’m not sure if it’s ok (it seems to be wrong actually).