# RubyCocoa question

**URL:** <https://www.ruby-forum.com/t/rubycocoa-question/164721>\
**Category:** Ruby\
**Created:** [April 4, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/rubycocoa-question/164721 "2009-04-04T18:57:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@abc](https://www.ruby-forum.com/u/abc)\
**Post date:** [April 4, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/rubycocoa-question/164721/1 "2009-04-04T18:57:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![abc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@abc](https://www.ruby-forum.com/u/abc)\
**Post date:** [April 4, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/rubycocoa-question/164721/2 "2009-04-04T19:45:31Z")

</div>

haven’t used rubycocoa, but I’ll try…😉

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?

---

<div class="post-metadata">

**Author:** ![abc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@abc](https://www.ruby-forum.com/u/abc)\
**Post date:** [April 4, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/rubycocoa-question/164721/3 "2009-04-04T20:40:36Z")

</div>

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).
