RubyCocoaã1æ¯
- 2008.07.02
- Ruby/Rails, åå¼·ä¼è³æ
ä»åã¯RubyCocoaã使ã£ã¦MacOSXã®ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ããã¨æãã¾ãã
RubyCocoaã¨ã¯
- Rubyã使ã£ã¦MacOSXã®ã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã§ããCocoaãæä½ã§ãã
- 対話çã«Cocoaãéçºã§ãã
- Leopardããæ¨æºæè¼
- ãã¼ã¸ã§ã³ 0.13.2(2008/2/16)
代表çãªã¢ããªã±ã¼ã·ã§ã³
RubyCocoaã使ã£ã¦éçºãããã¢ããªã±ã¼ã·ã§ã³ã«ã¯ä»¥ä¸ã®ãããªãã®ãããã¾ãã
- LimeChat for OSX
- Chemr
- [QSTwitter
](http://blog.deadbeaf.org/2008/03/01/qstwitter-14/)
ç¹ã«QuickSilverããTwitterã«æãããQSTwitterã¯ã¨ã¦ã便å©ã«ä½¿ããã¦ããã£ã¦ãã¾ãã
ä½ã£ã¦ã¿ãã
Twitterã«ãã¹ãããã ãã®ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ã¾ãã
æºå
Xcodeã使ãã®ã§ããããããã¤ã³ã¹ãã¼ã«ãã¦ããå¿ è¦ãããã¾ããMacOSXã®ã¤ã³ã¹ãã¼ã«DVDã«å«ã¾ãã¦ããã®ã§ããããã¤ã³ã¹ãã¼ã«ããããããã㯠ADC(Apple Developer Connection)ã®ãµã¤ããããã¦ã³ãã¼ããããã¨ãã§ãã¾ããã¾ããRubyããTwitterã«ãã¹ãããé¨åã§Ruby Twitter Gemã使ãã¾ããã®ã§ããããã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã ã¿ã¼ããã«ãéãã¦ã
$ sudo gem install twitter
ã§ã¤ã³ã¹ãã¼ã«ãã¾ãã
ããã¸ã§ã¯ãä½æ
ã¾ããããã¸ã§ã¯ããä½æãã¾ãã/Developer/Applications/Xcode.appãéãã¾ãã File->New Projectãé¸æãã Cocoa-Ruby Applicationãé¸æãã¾ã(1)ã
ããã¸ã§ã¯ãåãå ¥åãã¦æ¬¡ã«é²ãã¨ãããã¸ã§ã¯ãã¦ã£ã³ãã¦ãç«ã¡ãããã¾ã(2)ã
次ã«ã³ã³ããã¼ã©ãä½æãã¾ãã Classes->Add->Ruby NSObject subclass ãé¸æãã¾ã(3)ã
(3) ããã§ã¯AppController.rbã¨ãããã¡ã¤ã«åã§ä¿åãã¾ãã ä»åã¯æ¬¡ã®ãããªã³ã¼ãã«ãã¾ããã
require 'osx/cocoa'
require 'rubygems'
require 'twitter'
class AppController < OSX::NSObject
include OSX
ib_outlet :window, :name, :password, :message
ib_action :post
def post
twit = Twitter::Base.new(@name.stringValue.to_s, @password.stringValue.to_s)
twit.update(@message.stringValue.to_s)
@message.setStringValue ''
end
end
ib_outletã§ãã³ã³ããã¼ã©ããã¦ã£ã³ãã¦ã¸ã®åç §ãå®ç¾©ãã¦ãã¾ãã ib_actionã§ãã¢ã¯ã·ã§ã³ãå®ç¾©ãã¾ãã
ãã¿ã³ãã¯ãªãã¯ãããã¨ãã«ããã§å®ç¾©ããã¢ã¯ã· ã§ã³ãå¼ã°ããããã«å¾ã§è¨å®ãã¾ãã def post ⦠end ã«ã¢ã¯ã·ã§ã³postã®å¦çå 容ãæ¸ãã¾ãã Twitterã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã¦ã£ã³ãã¦ã«å ¥åãã¦ããã¡ãã»ã¼ã¸ããã¹ããã¾ãã@nameã§åç §ãã¦ãããªãã¸ã§ã¯ããåå¾ããstringValueã¡ã½ããã§ãã®ãªãã¸ã§ã¯ãã®æååãåå¾ãã¾ãã stringValueã§åå¾ããæååã¯Cocoaã®æååã¯ã©ã¹ã®ãªãã¸ã§ã¯ããªã®ã§ãto_sã¡ã½ãã㧠Rubyã®æååã«å¤æãã¦ãã¾ãã
setStringValueã¡ã½ããã§ãåç §ãã¦ãããªãã¸ã§ã¯ãã®æååã«æåãè¨å®ãã¦ãã¾ãã
ãã®ã³ã¼ããä¿åãã次ã«InterfaceBuilderã使ã£ã¦UIã¨ã³ã³ããã¼ã©ãçµã³ã¤ãã¦ããã¾ãã ããã¸ã§ã¯ãã¦ã£ã³ãã¦ã®MainMenu.nibãããã«ã¯ãªãã¯ãã(4)ã¨InterfaceBuilderãç«ã¡ä¸ããã¾ãã
(4) Libraryãã¬ããããNSObjectããã©ãã°ããã¡ã¤ã³ã¦ã£ã³ãã¦ã«ãããããã¾ã(5)ã
(5) ãã®ãªãã¸ã§ã¯ããé¸æããç¶æ ã§ãã¯ã©ã¹ãã¬ããã®Classã«å ç¨ä½æããAppControllerãé¸æãã¾ã(6)ã
(6) 次ã«Windowã¨ã³ã³ããã¼ã©ãçµã³ã¤ãã¦ããã¾ããä»åã¯å ã ããWindowã¯ä½¿ããªãã®ã§ãã¡ã¤ã³ã¦ã£ã³ãã¦ã«ããWindowãåé¤ãã¾ãã Libraryãã¬ããããHUD Windowããã©ãã°&ããããã§ã¡ã¤ã³ã¦ã£ã³ãã¦ã«ç§»åãã¾ã(7)ã
ãã®ã¦ã£ã³ãã¦ã¨ã³ã³ããã¼ã©ãçµã³ã¤ããããã«ãã¡ã¤ã³ã¦ã£ã³ãã¦ã®AppControllerãCtrlãã¼ãæ¼ããã¾ã¾ãã¦ã¹ãæ¼ããWindowã«ãã©ãã°ãã¦é¢ãã¾ãã
Outletsã¨ããã¦ã£ã³ãã¦ã表示ãããã®ã§ãAppController.rbã®ib_outletã«å®ç¾©ãã windowãé¸æãã¾ã(8)ã次ã«ãTwitterã«ãã¹ãããããã«å¿ è¦ãªãã¿ã³ãããã¹ããã£ã¼ã«ãã追å ãã¦ããã¾ãã ã¡ã¤ã³ã¦ã£ã³ãã¦ã®Panel(Window)ãããã«ã¯ãªãã¯ããã¨ãã®ã¦ã£ã³ãã¦ã表示ãããã®ã§ãããã«ããã«ã¦ã£ã³ãã¦ããå¿ è¦ãªãªãã¸ã§ã¯ãããã©ãã°&ããããã§é ç½®ãã¦ããã¾ã(9)ãä»åã¯ãPushButtonã¨TextField,Secure TextField, Labelã使ãã¾ããã
(9) é ç½®ããããã¹ããã£ã¼ã«ãããã¿ã³ã¨ã³ã³ããã¼ã©ãçµã³ã¤ãã¦ããã¾ãã ã¡ã¤ã³ã¦ã£ã³ãã¦ã®AppControllerãé¸æããCtrlãã¼ãæ¼ããã¾ã¾ãã¦ã¹ãæ¼ãã¦ã¦ã£ã³ãã¦ã®ãªãã¸ã§ã¯ãã«ãã©ãã°ãã¦é¢ãã¾ããããã¨ãé¸æè¢ã«AppController.rbã§å®ç¾©ããã¢ã¦ãã¬ããã表示ãããã®ã§ã対å¿ãããã®ãé¸æãã¾ã(10)ã
(10) messageãname,passwordã®é¨åããã®ããã«è¨å®ãã¦ããã¾ãã 次ã¯Twitterã«ãã¹ãããæã«æ¼ããã¿ã³ã¨ã³ã³ããã¼ã©ãçµã³ã¤ãã¾ãã ã¢ã¦ãã¬ããã®æã¨ã¯éã«ããã¿ã³ãé¸æãCtrlãã¼ãæ¼ããã¾ã¾ãã¦ã¹ãæ¼ãã¦ã¡ã¤ã³ã¦ã£ã³ãã¦ã®AppControllerã«ãã©ãã°ãã¦é¢ãã¾ã(11)ã ãã®ã¨ããAppController.rbã®ib_actionã«å®ç¾©ãããpostãã表示ãããã®ã§ãé¸æãã¾ãã
(11) ã¦ã£ã³ãã¦ã®ãªãã¸ã§ã¯ãããã¹ã¦ã³ã³ããã¼ã©ã¨çµã³ã¤ããããå®è¡ãã¦ã¿ã¾ãã Xcodeã®ã¡ãã¥ã¼ã«ãããBuild -> Build and Go ã§å®è¡ããã¨ã¢ããªã±ã¼ã·ã§ã³ãç«ã¡ä¸ããã¾ã(12)ã
Twitterã®ã¢ã«ã¦ã³ãæ å ±ãå ¥åããPostãã¿ã³ãæ¼ãã¦Messageãæãããã¨ãã§ããã°æåã§ãã
ãããã«
ç´°ããªå¦çã¯çç¥ãã¾ããããRubyCocoaã使ã£ã¦ã¢ããªã±ã¼ã·ã§ã³ãä½ã大ã¾ããªæµãã¯ã¤ãããã¨æãã¾ãã ã¢ã«ã¦ã³ãæ å ±ãééã£ã¦ããå ´åã®ä¾å¤å¦çããããããè¨å®é¨åã¯ç°å¢è¨å®ç»é¢ã«ç½®ããªã©ãã¾ã ã¾ã ãããã¨ã¯ããããããããã§ãã