OS X에 놀러간 루비 이야기

Preview:

DESCRIPTION

OSXDEV Bootcamp에서 발표한 내용입니다

Citation preview

OS X에 놀러간 루비 이야기

deepblue @ openmaru

1

2

루비

루비 온 레일스

루비 코코아

맥 루비

3

• 루비 1.8.6 p36+

• 루비젬• 레일스 1.2.3

• 루비 코코아 0.12.0

• DTrace

• BridgeSupport

• ScriptBridge

4

5

puts 'Hello Ruby!'

6

class Greeter def initialize(name) @name = name.capitalize end

def salute puts "Hello #{@name}!" endend

g = Greeter.new("world")g.salute

7

Objective-C와 루비

• 스몰토크• 카테고리 / 열린 클래스

• 오리 타이핑

8

class Fixnum def -(op) self + op endend

1 - 1

1 - 1 #=> 0

#=> 2

9

for i in (1..3) do puts iend

(1..3).each {|i| puts i}

1.upto(3) {|i| puts i}

11

•happiness•sustainable productivity•beautiful code

13

14

for iPhone WebApp

• 최고의 MVC 웹 프레임워크

• Resource + Multi View

• Plugin: rails_iui

15

16

왜 루비를?

• 재미, 생산성

• GC

• DSL -> 적은 코드

• attr_accessor :foo

• 익숙한 문법

• 스몰토크에 더 가깝다

18

어떻게?

• Objective-C 객체를 내부적으로 관리

• 프록시• 메시지와 예외를 전달

• 객체 변환

19

NSString *stringURL = @"http://deepblue.springnote.com/";

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:stringURL]

];

url = NSURL.URLWithString('http://deepblue.springnote.com')

NSWorkSpace.sharedWorkspace.openURL(url)

20

[coder encodeObject:personName forKey:@'personName'];

coder.encodeObject_forKey(personName, 'personName')

21

http://rucola.rubyforge.org/22

Rucola

• The Rails Way

• 디렉토리 구조 관례

• 명령행과 코드 생성기

• DSL

• Textmate!

• TDD / BDD

23

24

25

26

27

notify_on('LoginSuccess') {|noti| check_main_window(true)

}

@implementation Foo -(id)init { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:(loginSuceess:)

name:@"LoginSuccessNotification" object:nil]; } return self; } -(void)loginSuccess:(NSNotification *) notification { [self checkMainWindow:True] }@end

28

describe 'ApplicationController' do before do @controller = ApplicationController.alloc.init end it "should set itself as the application delegate" do OSX::NSApp.expects(:delegate=).with(@controller) @controller.ib_outlet(:main_window).

expects(:inspect) @controller.awakeFromNib endend

29

DEMO - DropBox

30

31

ib_outlet :url_text, :import_checkbox, :status_image, :background

ib_action :mode_changeddef mode_changed(sender) @status_image.image = load_image(import_mode? ? 'import' : 'attachment')end

def import_mode? @import_checkbox.state.to_i == 1end

def load_image(name) @image_loaded ||= {} return @image_loaded[name] if @image_loaded[name] bundle = OSX::NSBundle.mainBundle path = bundle.pathForImageResource(name) OSX::NSImage.alloc.initWithContentsOfFile(path)end

32

def register_drop_target @background.register_drop self @status_image.register_drop selfend

33

class DropArea < OSX::NSImageView def register_drop(controller) @controller = controller registerForDraggedTypes available_types end

# .... protected def available_types OSX::NSArray.arrayWithObjects(OSX::NSFilenamesPboardType, nil) endend

34

def performDragOperation(sender) pb = sender.draggingPasteboard return false unless pb.availableTypeFromArray(available_types) paths = pb.propertyListForType(OSX::NSFilenamesPboardType).to_ruby paths.each do |path| @controller.import_mode? ? import(path) : attach(path) end if paths.respond_to?(:to_ary) trueend

35

def import(path) url = @controller.springnote_client.import(path) url_obj = OSX::NSURL.URLWithString(url) OSX::NSWorkspace.sharedWorkspace.openURL url_objend

def attach(path) @controller.springnote_client.attach(path)end

36

나머지 코드• OAuth

• KeyChain

• SecKeychainAddGenericPassword

• SecKeychainFindGenericPassword

• SpringnoteResources

• visit http://myruby.net/

37

http://programming.nu/rubycocoa-and-rubyobjc

가끔은...

•두개의 언어가 섞여있다•이름 공간 충돌•객체 변환의 부담•객체 모델의 중복•Thread•GC

38

• a port of Ruby 1.9 (with YARV)

• for the Objective-C runtime and GC

• Ruby objects = Objective-C objects

• Keyed Arguments

MacRuby

http://ruby.macosforge.org/39

Links

• http://myruby.net/

• http://rubycocoa.springnote.com/

• http://rubycocoa.sourceforge.net/

• http://del.icio.us/byblue/ruby+cocoa

40

Q & A

41