Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyConf 2010 - MacRuby: Why and How

RubyConf 2010 - MacRuby: Why and How

What’s new since RubyConf 2009
See some examples of how fun it is to hack with MacRuby

Summary and more information:
http://matt.aimonetti.net/posts/2010/11/12/rubyconf-2010-macruby-talk/

Video:
http://www.confreaks.com/videos/448-rubyconf2010-macruby-why-and-how

Matt Aimonetti

November 12, 2010
Tweet

More Decks by Matt Aimonetti

Other Decks in Programming

Transcript

  1. MACRUBY A p p l e ’ s R u

    b y M a t t A i m o n e t t i - R u b y C o n f X - N o v 2 0 1 0 Friday, November 12, 2010
  2. MACRUBY A p p l e ’ s R u

    b y Friday, November 12, 2010
  3. MacRuby LLVM/Roxor Parser Stdlib AOT JIT Obj-C Runtime libobjc libauto

    Builtin Classes CoreFoundation MacRuby Friday, November 12, 2010
  4. MacRuby LLVM/Roxor Parser Stdlib AOT JIT Obj-C Runtime libobjc libauto

    Builtin Classes CoreFoundation YARV Parser GC Runtime Stdlib Builtin classes Ruby 1.9 Friday, November 12, 2010
  5. JIT Compilation MacRuby Ruby AST LLVM IR Machine Code CPU

    Transforms the AST into LLVM intermediate representation ... optimizes it ... compiles it to machine code ... and executes it! Very good performance for algorithmic operations Friday, November 12, 2010
  6. AOT Compilation MacRuby Ruby AST LLVM IR Machine Code Mach-O

    Object File CLI tool or Xcode ... applies more optimizations ... and saves the machine code to disk ... faster bootstrap time Can be used to obfuscate the source code Friday, November 12, 2010
  7. framework 'AppKit' class AppDelegate def applicationDidFinishLaunching(notification) voice_type = "com.apple.speech.synthesis.voice.GoodNews" @voice

    = NSSpeechSynthesizer.alloc.initWithVoice(voice_type) end def windowWillClose(notification) puts "Bye!" exit end def say_hello(sender) @voice.startSpeakingString("Hello World!") puts "Hello World!" end end app = NSApplication.sharedApplication app.delegate = AppDelegate.new window = NSWindow.alloc.initWithContentRect([200, 300, 300, 100], styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask, backing:NSBackingStoreBuffered, defer:false) window.title = 'MacRuby: The Definitive Guide' window.level = 3 window.delegate = app.delegate button = NSButton.alloc.initWithFrame([80, 10, 120, 80]) button.bezelStyle = 4 button.title = 'Hello World!' button.target = app.delegate button.action = 'say_hello:' window.contentView.addSubview(button) window.display window.orderFrontRegardless app.run hello_world.rb Friday, November 12, 2010
  8. $ macrubyc hello_world.rb -o demo $ file demo demo: Mach-O

    64-bit executable x86_64 $ ./demo Compile to machine code Friday, November 12, 2010
  9. Create compiled/ dynamic libraries $ macrubyc file1.rb file2.rb -o shared.dylib

    --dylib $ macrubyc other_project_source.rb shared.dylib -o example $ ./example Friday, November 12, 2010
  10. characters = %w{Adama Apollo Baltar Roslin StarBuck Six} def cylon?(character)

    false # buggy detector end characters.each do |character| if cylon?(character) puts "#{character} is a Cylon!" else puts "#{character} is not a cylon." end end Friday, November 12, 2010
  11. $ macruby cylon_detector.rb Adama is not a cylon. Apollo is

    not a cylon. Baltar is not a cylon. Roslin is not a cylon. StarBuck is not a cylon. Six is not a cylon. Buggy code :( Friday, November 12, 2010
  12. $ macrubyd cylon_detector.rb Starting program. 1! characters = %w{Adama Apollo

    Baltar Roslin StarBuck Six} cylon_detector.rb:1> Debug the detector live Friday, November 12, 2010
  13. cylon_detector.rb:1> b cylon_detector.rb:8 if character == 'Six' Added breakpoint 1.

    Added a breakpoint on a speci c le/line based on a condition Friday, November 12, 2010
  14. cylon_detector.rb:1> c Adama is not a cylon. Apollo is not

    a cylon. Baltar is not a cylon. Roslin is not a cylon. StarBuck is not a cylon. 8! if cylon?(character) cylon_detector.rb:8> Breaks on condition Friday, November 12, 2010
  15. Good but not good enough, threads are still a pain

    in the butt! Friday, November 12, 2010
  16. Let GCD gure out how many threads to use! https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

    require 'dispatch' job = Dispatch::Job.new { slow_operation } job.value # => “wait for the result” Friday, November 12, 2010
  17. Elegant wrapper API https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/ require 'dispatch' job = Dispatch::Job.new job.add

    { slow_operation(a) } job.add { slow_operation(b) } job.add { slow_operation(c) } job.join job.values # =>[result_a, result_b, result_c] Friday, November 12, 2010
  18. https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/ require 'dispatch' job = Dispatch::Job.new @scores = job.synchronize Hash.new

    [user1, user2, user3].each do |user| job.add \ { @scores[user.name] = calculate(user) } end job.join @scores[user1.name] # => 1042 @scores[user2.name] # => 673 @scores[user3.name] # => 845 Friday, November 12, 2010
  19. https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/ require 'dispatch' job = Dispatch::Job.new @scores = job.synchronize Hash.new

    [user1, user2, user3].each do |user| job.add \ { @scores[user.name] = calculate(user) } end job.join @scores[user1.name] # => 1042 @scores[user2.name] # => 673 @scores[user3.name] # => 845 safe proxy object Friday, November 12, 2010
  20. $ macirb --simple-prompt >> require 'open-uri' => true >> begin

    >> Sandbox.no_internet.apply! >> open('http://www.macruby.org') >> rescue SystemCallError => exception >> puts exception >> end Operation not permitted - connect(2) => nil Friday, November 12, 2010
  21. framework 'Foundation' class String def language CFStringTokenizerCopyBestStringLanguage(self, CFRangeMake(0, self.size)) end

    end ["Bonne année!", "Happy new year!", "¡Feliz año nuevo!", "Felice anno nuovo!", "ةﺪﻴﻌﺳ دﺎﻴﻋأ", "໌͚·͓ͯ͠ΊͰͱ͏͍͟͝·͢ɻ"].each do |msg| puts "#{msg} (#{msg.language})" end Friday, November 12, 2010
  22. Bonne année! (fr) Happy new year! (en) ¡Feliz año nuevo!

    (es) Felice anno nuovo! (it) (ar) ةﺪﻴﻌﺳ دﺎﻴﻋأ ໌͚·͓ͯ͠ΊͰͱ͏͍͟͝·͢ɻ (ja) Friday, November 12, 2010