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

Ruby - Write Once, Run Anywhere

Ruby - Write Once, Run Anywhere

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

September 12, 2014
Tweet

More Decks by Michał Taszycki

Other Decks in Programming

Transcript

  1. Opal.rb • Ruby to Javascript compiler • Performant • Version

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

    will be available soon • Native performance • Version > 2.0, actively developed • Widely used in production
  3. 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
  4. Differences • No require at runtime • No eval at

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

    • symbols are strings • regexps differ • All numbers are floats • no for loop yet ;)
  6. 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)
  7. This is your dreamland! Internal interactions DCI Hexagonal Clean Architecture

    DCI DDD Cowboy Coding Functional Programming CQRS Micro Libraries
  8. 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)
  9. 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)
  10. ! ! ! # We want to save it afterwards

    on each platform todo.mark_as_done Async interactions
  11. 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
  12. Abstract interface Todo.saver = TodoSaver.new(DatabaseAdapter.new) todo.mark_as_done ! # or !

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

    about use cases +Useful when use cases on each platform are similar
  14. 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
  15. 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
  16. 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
  17. Cross Platform Gems • Tell sprockets where to look for

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

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

    http://blog.crossplatformruby.com/universal-ruby-gems-in-20-lines-of-code
  20. Suggestions 1.Make a folder named as an app 2.Rename app

    folders to platform names 3.Keep them together with gems 4.Keep each of those in separate repositories 5.Use semantic versioning (if you work in teams) 6.Break Replace rules with better ones