mmmm..mruby everywhere & revisiting Ruby

mmmm..mruby everywhere & revisiting Ruby

Aloha RubyConf 2012 talk covering mruby the minimalistic and embedded version of Ruby by Matz as well as a look at Ruby from a functional perspective.
For a concrete example of how to easily use mruby, see this Rakefile example: https://gist.github.com/3850240
Video: http://confreaks.com/videos/1252-aloharuby2012-mmm-mruby-or-why-yet-another-ruby-implementation

C69521d6e22fc0bbd69337ec8b1698df?s=128

Matt Aimonetti

October 10, 2012
Tweet

Transcript

  1. mmm.. mruby Aloha RubyConf 2012 - Matt Aimonetti @merbist Tuesday,

    October 9, 12
  2. revisit ruby Tuesday, October 9, 12

  3. Nico disclaimer Tuesday, October 9, 12

  4. Nico Tuesday, October 9, 12

  5. Tuesday, October 9, 12

  6. Tuesday, October 9, 12

  7. “now” hiring Tuesday, October 9, 12

  8. 1. walk around 2. ask the audience questions 3. don't

    read your content 4. change your tone of voice frequently 5. have a mai-tai before you start? 6. enjoy yourself Tuesday, October 9, 12
  9. ruby Tuesday, October 9, 12

  10. JRuby C Ruby REE rubinus MacRuby IronRuby Maglev Tuesday, October

    9, 12
  11. Tuesday, October 9, 12

  12. mruby Tuesday, October 9, 12

  13. why? Tuesday, October 9, 12

  14. modular Tuesday, October 9, 12

  15. float instead of double max func args disable regexp disable

    sprintf disable math disable time disable stdio memory management Tuesday, October 9, 12
  16. anything that isn’t cross platform isn’t available Tuesday, October 9,

    12
  17. embedded Tuesday, October 9, 12

  18. Lego Mindstorms NXT 64kb RAM Tuesday, October 9, 12

  19. mruby != ruby 2.0 Tuesday, October 9, 12

  20. Tuesday, October 9, 12

  21. matz’ embeddable minimal implementation of the ruby language mmmmm mruby

    Tuesday, October 9, 12
  22. minimalistic ruby Tuesday, October 9, 12

  23. Tuesday, October 9, 12

  24. mruby preliminary release Tuesday, October 9, 12

  25. Regional Innovation Creation R&D Program Tuesday, October 9, 12

  26. execution Tuesday, October 9, 12

  27. • $ mruby hello_world.rb execution Tuesday, October 9, 12

  28. • $ mruby hello_world.rb • REPL / mirb execution Tuesday,

    October 9, 12
  29. • $ mruby hello_world.rb • REPL / mirb • $

    mruby -e “p ‘Hello World!’ ” execution Tuesday, October 9, 12
  30. • $ mruby hello_world.rb • REPL / mirb • $

    mruby -e “p ‘Hello World!’ ” • bytecode (intermediate representation) execution Tuesday, October 9, 12
  31. • $ mruby hello_world.rb • REPL / mirb • $

    mruby -e “p ‘Hello World!’ ” • bytecode (intermediate representation) • convert to C code (readable or binary) execution Tuesday, October 9, 12
  32. 2012 #include  <stdlib.h> #include  <stdio.h> /*  Include  the  needed  mruby

     headers  */ #include  <mruby.h> #include  <mruby/compile.h> int  main(void) {    mrb_state  *mrb  =  mrb_open();    char  code[]  =  "p  'hello  world!'";    printf("Executing  Ruby  code  from  C!\n");    mrb_load_string(mrb,  code);    return  0; } Tuesday, October 9, 12
  33. 2012 $  gcc  -­‐Iinclude  hello.c  lib/libmruby.a  -­‐lm  -­‐o   hello.out

    $  ./hello.out Executing  Ruby  code  from  C! "hello  world!" Tuesday, October 9, 12
  34. Use case(s)? Tuesday, October 9, 12

  35. anywhere Lua is used Tuesday, October 9, 12

  36. Tuesday, October 9, 12

  37. Lua combines simple procedural syntax with powerful data description constructs

    based on associative arrays and extensible semantics. Tuesday, October 9, 12
  38. Ruby is a dynamic programming language with a focus on

    simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. Tuesday, October 9, 12
  39. 2009 survey - scripting languages used by game developers Tuesday,

    October 9, 12
  40. small footprint portable very easily embedded Tuesday, October 9, 12

  41. “Lua mruby is dynamically typed, runs by interpreting bytecode for

    a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.” Tuesday, October 9, 12
  42. mobiruby Tuesday, October 9, 12

  43. http://mobiruby.org/ Tuesday, October 9, 12

  44. ruby for node.js https://github.com/mattn/mruby-uv Tuesday, October 9, 12

  45. mix JS & Ruby using Node.js Tuesday, October 9, 12

  46. mod_mruby https://github.com/matsumoto-r/mod_mruby Tuesday, October 9, 12

  47. backends  =  [        "http://192.168.0.101:8888/",      

     "http://192.168.0.102:8888/",        "http://192.168.0.103:8888/",        "http://192.168.0.104:8888/", ] #  write  balancing  algorithm  here. r  =  Apache::Request.new() r.handler    =  "proxy-­‐server" r.proxyreq  =  Apache::PROXYREQ_REVERSE r.filename  =  "proxy:"  +                          backends[rand(backends.length)]  +  r.uri Apache::return(Apache::OK) Tuesday, October 9, 12
  48. Tuesday, October 9, 12

  49. Gotchas Tuesday, October 9, 12

  50. no fork no threads Tuesday, October 9, 12

  51. no “require” Tuesday, October 9, 12

  52. not officially released Tuesday, October 9, 12

  53. very little documentation Tuesday, October 9, 12

  54. no std lib Tuesday, October 9, 12

  55. https:// github.com/ iij/mruby mruby extension for unix Tuesday, October 9,

    12
  56. • Digest::MD5/RIPEMD160/SHA1/SHA256/SHA384/SHA512 • Digest::HMAC: #reset • ENV: ::[] :: []=

    ::clear ::delete ::inspect ::keys ::size ::store ::to_a ::to_hash ::to_s ::values • Errno::EXXX • File: ::open ::umask ::unlink ::delete ::rename ::exist? ::exists? ::dirname #path • IO: ::open ::sysopen ::popen #close #closed? #each #each_byte #each_line #read #sync #sync= #write #to_io • Regexp: ::compile ::last_match #match • TCPSocket: ::new ::open • UNIXSocket: #addr ::new ::open #peeraddr • Syslog: ::open ::close ::log ::opened? ::ident ::options ::facility • SystemCallError • Kernel: #exit #sleep #system • (and more, including features that should land in mruby master) Tuesday, October 9, 12
  57. so how fast is it? Tuesday, October 9, 12

  58. Benchmark time!!! Tuesday, October 9, 12

  59. ruby is a hacking language Tuesday, October 9, 12

  60. use ruby everywhere! Tuesday, October 9, 12

  61. Tuesday, October 9, 12

  62. Tuesday, October 9, 12

  63. Tuesday, October 9, 12

  64. Tuesday, October 9, 12

  65. Tuesday, October 9, 12

  66. Tuesday, October 9, 12

  67. ruby everywhere! not just for the web Tuesday, October 9,

    12
  68. Questions? Tuesday, October 9, 12

  69. revisit ruby (talk part deux) Tuesday, October 9, 12

  70. functional AND object oriented Tuesday, October 9, 12

  71. functional! Tuesday, October 9, 12

  72. functional freedom programming Tuesday, October 9, 12

  73. no side effects Tuesday, October 9, 12

  74. higher order functions Tuesday, October 9, 12

  75. composability Tuesday, October 9, 12

  76. (1..17).select(&:odd?). map(&:to_f). map do |x| (x / rand(99)).rationalize end #

    => [(1/61), (3/68), (5/22), (7/45), (9/68), (11/23), (1/6), (15/46), (17/13)] Tuesday, October 9, 12
  77. Enumerator:: Lazy Tuesday, October 9, 12

  78. currying Tuesday, October 9, 12

  79. sum = -> func,a,b do result = 0 a.upto(b){|n| result

    += func.(n) } result end # currying curried_sum = sum.curry # partial functions sum_ints = curried_sum[-> x {x}] sum_of_squares = curried_sum[-> x {x**2}] sum_of_powers_of_2 = curried_sum[-> x {2**x}] # results sum_ints[1,5] #=> 15 sum_of_squares.call(1,5) #=> 55 sum_of_powers_of_2.(1,5) #=> 62 Tuesday, October 9, 12
  80. TCO Tuesday, October 9, 12

  81. RubyVM::InstructionSequence.\ compile_option[:tailcall_optimization] Tuesday, October 9, 12

  82. vm_opts.h #define OPT_TAILCALL_OPTIMIZATION 1 Tuesday, October 9, 12

  83. def fact(n, r=1) if n < 2 r else fact(n-1,

    n*r) end end fact(30_000) # => SystemStackError: stack level too deep Tuesday, October 9, 12
  84. option = { tailcall_optimization: true, trace_instruction: false, } iseq =

    RubyVM::InstructionSequence.new(<<-EOF, "fact.rb", '/', nil, option).eval class Tailcall def fact(n, r=1) if n < 2 r else fact(n-1, n*r) end end end EOF Tailcall.new.fact(30_000) Tuesday, October 9, 12
  85. pattern matching Tuesday, October 9, 12

  86. me = ["Matt",42, "San Diego"] awesome_places = ["San Diego", "Hawaii"]

    case me when -> list {seq.first == "matt" } puts "Hey #{list.first} from #{list.last}" when -> list {awesome_places.include?(seq.last)} puts "Yay #{list.first}, you live in \ an awesome place" else puts ":(" end Tuesday, October 9, 12
  87. immutability Tuesday, October 9, 12

  88. not a requirement to be FP Tuesday, October 9, 12

  89. modules don’t have to be mixins Tuesday, October 9, 12

  90. module RubyConf module_function def generate_oauth_token(id, scope, expiration) "#{id}#{scope}#{expiration}" end def

    valid_headers?(headers) true end def set_sso_cookies!(response) response.set_cookie 'sso', {value: '123', domain: TLD} response end end Tuesday, October 9, 12
  91. Ruby not really optimized for FP (yet) Tuesday, October 9,

    12
  92. ruby everywhere! Tuesday, October 9, 12

  93. “ruby” developers Tuesday, October 9, 12

  94. product builders Tuesday, October 9, 12

  95. you’re also hackers! Tuesday, October 9, 12

  96. have fun! Tuesday, October 9, 12

  97. play with ruby everywhere! Tuesday, October 9, 12

  98. matt aimonetti @ m e r b i s t

    Tuesday, October 9, 12