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

MobiRuby introduction

MobiRuby introduction

Yuichiro MASUI

September 14, 2012
Tweet

More Decks by Yuichiro MASUI

Other Decks in Programming

Transcript

  1. MobiRuby
    iOS + Ruby = Super Awesome!!

    View Slide

  2. Hi! I’m masuidrive.
    • Yuichiro MASUI (masuidrive, Ichi)
    • Open source developer, Ruby fun
    • FrogApps, Inc. ex-Appcelerati
    • Pukiwiki, Ruby on Rails tutorial movie in
    Japanese, IKEA hacker
    • Furo-grammer (Coding in hot tub)

    View Slide

  3. What’s MobiRuby?
    • iOS app development environment on
    mruby
    • Can access native functions
    • Have plan for Android version

    View Slide

  4. Demo Apps
    • MobiRuby Game

    View Slide

  5. Vision
    • MobiRuby provides Ruby power to Mobile
    devices
    • DSL is most important Ruby power

    View Slide

  6. How you can get it
    • http://mobiruby.org/
    • It’s initial and truly ALPHA version, so it’s
    for only mruby and iOS hackers.

    View Slide

  7. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  8. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  9. mruby
    • New implementation of Ruby for embedding
    • Built by dad Matz
    • Less memory and storage
    • Not required POSIX, only C99
    • Simple spec, Not included File, Socket,
    Thread and ext libraries

    View Slide

  10. View Slide

  11. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  12. mruby-cfunc
    • C function bridge for mruby
    • Call C based function directly
    • Based on libFFI
    • Like DL library on CRuby

    View Slide

  13. mruby-cfunc
    str  =  "STRING"
    CFunc::call(CFunc::Void,  "puts",  str)
    STRING

    View Slide

  14. mruby-cfunc
    str  =  "STRING"
    ptr  =  CFunc::Pointer.malloc(7)
    result_ptr  =  CFunc::call(CFunc::Pointer,  
    "strcpy",  ptr,  str)
    ptr.to_s
    -­‐>  “STRING”

    View Slide

  15. mruby-cfunc
    \
    class  TestStruct  <  CFunc::Struct
           define  CFunc::SInt8,    :x,
                         CFunc::SInt16,  :y
    end
    test  =  TestStruct.new
    test[:x]  =  10

    View Slide

  16. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  17. mruby-cocoa
    • Cocoa bridge for mruby
    • Use Cocoa functions transparently
    • Based on mruby-cfunc and Cocoa runtime
    • Manipulate Cocoa objects
    • Create class/instance, inherit existing classes
    • Garbage collection

    View Slide

  18. Bridge Cocoa runtime
    • Objective-C has powerful runtime features
    • Create and modify class dynamically
    • ObjC class <- Ruby class <- ObjC class
    • ObjC value interconverted with Ruby value
    • Supported delegate and blocks

    View Slide

  19. Memory
    management
    • Objective-C - Reference count
    • mruby - Mark & Sweep
    • Swizzled Objective-C release method

    View Slide

  20. Multi-threading
    • MobiRuby does not support threads
    • Because mruby does not support threads
    • I have not touched them yet
    • Need to implement multi VM instead of
    thread

    View Slide

  21. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  22. mobiruby-ios
    • iOS specific utilities
    • Bootstrap
    • Xcode integration
    • Wrapped classes

    View Slide

  23. Wrapped Audio Player
    class  AudioPlayer
           def  initialize(filename)
                   name,  ext  =  filename.split(".")
                   path  =  Cocoa::NSBundle._mainBundle.
                         _pathForResource  name,  :ofType,  ext
                   url  =  Cocoa::NSURL._fileURLWithPath  path
                   @avap  =  Cocoa::AVAudioPlayer._alloc.
                     _initWithContentsOfURL  url,  :error,  nil
           end
           def  play;  @avap._play;  end
    end
    AudioPlayer.new("bgm00.wav").play

    View Slide

  24. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  25. mobiruby-common
    • Will be common utilities with Android ver.
    • require method
    • Some POSIX based functions

    View Slide

  26. MobiRuby stack
    iOS
    mruby
    mruby-
    cfunc
    Your code
    mruby-
    cocoa
    mobiruby-
    common
    mobiruby-
    ios

    View Slide

  27. Your code
    class  Cocoa::MyAlertView  <  Cocoa::UIAlertView
           define  C::Void,  :alertView,  Cocoa::Object,
               :clickedButtonAtIndex,  C::Int  do  |me,  index|
                   if  index.to_i  ==  1
                           app  =  Cocoa::UIApplication._sharedApplication
                           str  =  "http://mobiruby.org"
                           url  =  Cocoa::NSURL._URLWithString(str)
                           app._openURL  url
                   end
           end
    end

    View Slide

  28. Your code
    alert  =  Cocoa::MyAlertView._alloc._initWithTitle
       "Hello",
       :message,  "I  am  MobiRuby",
       :delegate,  nil,
       :cancelButtonTitle,  "I  know!",
       :otherButtonTitles,  "What's?",  nil
    alert._setDelegate  alert
    alert._show

    View Slide

  29. Road map
    • Finished building Cocoa bridge
    • Fix bugs, Improvement
    • Packaging
    • Documentation
    • Build wrapped APIs

    View Slide

  30. Road map
    • Finished building Cocoa bridge
    • Fix bugs, Improvement
    • Packaging
    • Documentation
    • Build wrapped APIs

    View Slide

  31. Road map
    • Finished building Cocoa bridge
    • Fix bugs, Improvement
    • Packaging
    • Documentation
    • Build wrapped APIs
    Version 1
    Q1 2013

    View Slide

  32. Progress in Sept 2012
    • Already MobiRuby based app is in AppStore
    • Finally released alpha 1
    • It's only for iOS and mruby hackers
    • Need deeply understanding Cocoa runtime,
    mruby, Garbage collection

    View Slide

  33. Current tasks
    • Fixed crashing bugs and memory leaks
    • Test Test Test!!!
    • Support `long` and `long long`, define Cocoa
    property and more...
    • Circular reference
    • mruby stability

    View Slide

  34. What’s difference?
    • RubyMotion
    • Rhodes
    • Ruboto
    • RubyCocoa
    • Titanium Mobile

    View Slide

  35. What’s difference?
    • RubyMotion
    • Rhodes
    • Ruboto
    • RubyCocoa
    • Titanium Mobile
    Diversity is Good

    View Slide

  36. Pros.
    • Ruby power
    • Based on Matz implemented Ruby
    • MIT license
    • Compact (~3000 lines)
    • An unexplored field

    View Slide

  37. Cons.
    • Buggy
    • Less classes / functions
    • Don't have debugging feature
    • Need to understand iOS and Cocoa

    View Slide

  38. FAQ
    • Can I use RubyGems?
    • Can I use meta programming?
    e.g., define_method, eval
    • Can I use exists Cocoa libraries?
    • Can I define new method to exist Cocoa class?
    • What version iOS does it support?

    View Slide

  39. Can I use RubyGems?
    • No, mruby doesn’t have compatibility with
    CRuby ext. APIs.
    • You need to write new extension for mruby

    View Slide

  40. Can I use meta programming?
    e.g., define_method, eval
    • Yes, mruby supports almost all dynamic
    programming features.
    • But MobiRuby doesn’t support eval. mruby
    can remove compiler. (it’s for Apple)

    View Slide

  41. Can I use existing
    Cocoa libraries?
    • Yes, you can use almost all existing libraries.
    • I think CocoaPods will be a good partner

    View Slide

  42. Can I define new method
    to exist Cocoa class?
    • Yes, You can define new methods from
    Ruby
    • You can inherit exists Cocoa and create new
    class from Ruby

    View Slide

  43. What version iOS
    does it support?
    • MobiRuby supports iOS4 and higher
    version.
    • I tested on iOS4 and iOS5. Probably run on
    iOS6.

    View Slide

  44. Wanted contributions
    • Here’s your big chance!
    • Need understanding C/Objective-C and iOS
    development
    • Owned OSX and iOS developer license

    View Slide

  45. Thank you
    • Logo design: Maylis Agniel
    • http://mobiruby.org
    • http://fb.me/mobiruby
    • http://twitter.com/mobiruby
    • http://github.com/mobiruby

    View Slide