---
title: "[心得] RubyCocoa"
author: "thegiive"
date: "2007-07-25T14:37:42Z"
tags: []
slug: "rubycocoa"
draft: false
lastmod: "2007-07-25T14:37:42Z"
id: 106221
url: "https://thegiive.iteye.com/blog/106221"
---

轉自 ptt MacDev 板

作者 godfat (godfat 真常) 看板 MacDev  
標題 \[心得] RubyCocoa  
時間 Tue Jul 24 21:40:20 2007  
──────────────────

嫌 objective-c 太難用嗎？還在等 obj-c 2.0 嗎？先試試 RubyCocoa 吧 XD  
http://rubycocoa.sourceforge.net/

\[quote]  
It lets you write full-stack Cocoa applications in Ruby. It is also possible to write a Cocoa application that mixes Ruby and Objective-C code.  
\[/quote]

how to install?  
有灌 macports 的話：（當然，xcode 是一定要灌的）  
sudo port install ruby  
sudo port install rb-rubygems（非必要，但建議）  
sudo port install rb-cocoa

文件放在：  
/Developer/Documentation/RubyCocoa

範例放在：（相當豐富）  
/Developer/Examples/RubyCocoa

我這裡有一個簡化的 hello world, 可以小瞥一下：

require 'osx/cocoa'  
include OSX

app = NSApplication.sharedApplication

hello = NSWindow.alloc.initWithContentRect\_styleMask\_backing\_defer \[200.0, 300.0, 100.0, 100.0], 15, 2, 0  
hello.setTitle 'Hello'

btn = NSButton.alloc.initWithFrame \[10.0, 10.0, 80.0, 80.0]  
hello.contentView.addSubview btn  
btn.setTitle 'Hello World!'  
btn.setAction 'stop:'  
btn.setBezelStyle 4

hello.display  
hello.orderFrontRegardless

app.run

另外可以順便請教一下，如何讓視窗在按下 X 時關閉而不是隱藏嗎？我沒寫過 obj-c（XD）, 昨天文件翻來翻去找不到要怎麼做

另一方面，如果只是要做 GUI 而不需要 call mac 的 api 的話，個人強力推薦 QtRuby

sudo port install qt4-mac（這個會 build 很久）  
sudo port install cmake

QtRuby 沒有這麼方便的安裝法，要到  
http://rubyforge.org/projects/korundum/  
抓 qt4-qtruby, 然後進到目錄下：

cmake .  
make  
sudo make install

於是就可以正常使用 QtRuby 了，hello world 如下：

require 'qt'  
include Qt  
app = Application.new ARGV  
hello = PushButton.new "Hello World!"  
hello.resize 100, 30  
hello.show  
app.exec

Qt 是什麼？  
http://trolltech.com/products/qt  
是我用過最棒的 GUI toolkit, 使用 C++ 語言。可用 GPL 授權或用商業授權（很貴），二擇一（這是 Qt 唯一被批評的點）

Ruby 是什麼？  
http://ruby-lang.org/  
是我用過最方便的 programming language, 使用 Ruby license（很自由）歡迎來 ptt Ruby 板參觀 :p

Mac OS 10.5 將搭載 Ruby 1.8.6 + Rails 1.2.3, 想試試 Rails 寫網站嗎？  
sudo gem install rails  
不需要等 10.5 就可以輕鬆試驗了 XD  
http://rubyonrails.org/

==

發完神經了，有傷眼請見諒 XD

--

```
#!/usr/bin/ruby          [露比] /Programming (Kn|N)ight/            看板《Ruby》
# if a dog nailed extra legs that          http://www.ptt.cc/bbs/Ruby/index.html
# walks like an octopus, and                            Welcome ~Ruby@ptt~
# talks like an octopus, then       ◢█◣            http://www.ruby-lang.org/
# we are happy to treat it as       █                http://www.ruby-doc.org/
# if it were an octopus.            ◥  ◤            http://www.rubyforge.org/
```

--  
※ 發信站: 批踢踢實業坊(ptt.cc)  
◆ From: 220.135.28.18