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

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

Matt Aimonetti

October 10, 2012
Tweet

More Decks by Matt Aimonetti

Other Decks in Programming

Transcript

  1. 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
  2. float instead of double max func args disable regexp disable

    sprintf disable math disable time disable stdio memory management Tuesday, October 9, 12
  3. • $ mruby hello_world.rb • REPL / mirb • $

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

    mruby -e “p ‘Hello World!’ ” • bytecode (intermediate representation) execution Tuesday, October 9, 12
  5. • $ 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
  6. 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
  7. 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
  8. Lua combines simple procedural syntax with powerful data description constructs

    based on associative arrays and extensible semantics. Tuesday, October 9, 12
  9. 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
  10. “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
  11. 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
  12. • 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
  13. (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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. matt aimonetti @ m e r b i s t

    Tuesday, October 9, 12