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

Ruby - Write Once, Run Anywhere - Polyconf 2014

Ruby - Write Once, Run Anywhere - Polyconf 2014

Some people say that Ruby is dying.
I wouldn't…
I'd rather claim that Ruby is more alive than ever!
Few years ago we could only envy Javascript programmers while their favorite language was becoming ubiquitous. Currently, thanks to RubyMotion we can write desktop and mobile apps. While Opal can execute Ruby in a browser. With these tools we can finally write cross-platform Ruby apps.
Learn how!

Michał Taszycki

October 31, 2014
Tweet

More Decks by Michał Taszycki

Other Decks in Programming

Transcript

  1. What does it mean? • Game logic required in each

    client • Game logic required on server
  2. Opal.rb • Ruby to Javascript compiler • Performant • Version

    < 1.0, actively developed • Out of the box Rails integration • Interesting libraries start to show up
  3. RubyMotion • Compiles Ruby to ObjectiveC runtime • Android Support

    available in beta • Native performance • Version > 2.0, actively developed • Widely used in production
  4. mruby • Supports ISO ruby standard • Can be embedded

    into any C application • low memory footprint • Faster than MRI • Works even on Arduino DUE (requires 96KB SRAM) • Promising for Windows desktop applications
  5. Differences • No require at runtime • No eval at

    runtime • Methods can have incompatible names • Threads are working • Memory leaks possible
  6. Differences • no require at runtime • strings are immutable

    • symbols are strings • regexps differ • All numbers are floats • no for loop yet ;)
  7. Differences • no require at runtime • gems (mrgems) need

    to be compiled into mruby • no Bignumber support • Regular Expressions unavailable by default • optimized for size (but ofter faster than MRI)
  8. This is your dreamland! Internal interactions DCI Hexagonal Clean Architecture

    DCI DDD Cowboy Coding Functional Programming CQRS Micro Libraries
  9. Access From Outside event_logger.save_event(:user_signed_up) event_logger.save_event(:user_tapped_on_top_left_button) event_logger.save_event(:user_bought_an_in_app_purchase, type: "coin", amount: 1000)

    event_logger.save_event(:user_signed_in) event_logger.save_event(:user_browsed_products) event_logger.save_event(:user_bought_product, name: "beef_jerky", amount: 35)
  10. Driving from inside # toggle panels - same interface #

    for animation on all platforms ! image_editor_controller.animate_view( brushes_panel, :horizontall_slide, from: 0, to: -90) ! image_editor_controller.animate_view( shapes_panel, :horizontall_slide, from: -90, to: 0)
  11. ! ! ! # We want to save it afterwards

    on each platform todo.mark_as_done Async interactions
  12. class Todo def self.saver=(saver) @saver = saver end ! def

    mark_as_done self.done = true self.class.saver.save(self) end end Abstract interface
  13. Abstract interface Todo.saver = TodoSaver.new(DatabaseAdapter.new) todo.mark_as_done ! # or !

    Todo.saver = TodoSaver.new(RestAdapter.new) todo.mark_as_done
  14. Abstract interface +Well defined interaction - Core needs to know

    about use cases +Useful when use cases on each platform are similar
  15. Notifications todo.on(:marked_as_done) do |todo| save_to_database(todo) end todo.mark_as_done ! # or

    ! todo.on(:marked_as_done) do |todo| post_to_api(todo) end todo.mark_as_done
  16. Notifications +Core doesn’t need to know use cases - Hard

    to compose interactions !Beware of memory leaks +Useful when use cases on each platform are a bit different
  17. AOP +Core is blissfully unaware of surroundings - Platforms need

    to know about internals !Beware of memory leaks +Useful when use cases on each platform are different
  18. Cross Platform Gems • Tell sprockets where to look for

    files using Opal.append_path • use require as usual (it is translated into Sprockets directive)
  19. Cross Platform Gems • Use require only in the gem’s

    entry point • Add all required files in rubymotion config block
  20. Cross Platform Gems It literally takes 20 lines of code

    http://blog.crossplatformruby.com/universal-ruby-gems-in-20-lines-of-code