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

LRUG mruby

LRUG mruby

A hands on overview for customizing and extending mruby with mrbgems and gemboxes.

More Decks by Randy Morgan (@morgan_randy)

Other Decks in Programming

Transcript

  1. Tuesday, May 14, 13

  2. mruby Tuesday, May 14, 13

  3. mruby Let’s get dirty Tuesday, May 14, 13

  4. Who are you? @morgan_randy github.com/randym reallyenglish.com Tuesday, May 14, 13

  5. What is mruby Embeddable Minimal Implementation OS/CPU autonomous (minimal ifdef)

    Limited CRuby (no IO / Socket / Thread / etc.) Aiming for ISO/IEC 30170:2012 compliance Tuesday, May 14, 13
  6. Choose the parts you need mrbgems gemboxes no require Tuesday,

    May 14, 13
  7. mruby-allegro.gem mruby-allocate.gem mruby-arduino.gem mruby-audite.gem mruby-avl.gem mruby-aws-s3.gem mruby-backtrace.gem mruby-base64.gem mruby-capability.gem mruby-cfunc.gem

    mruby-cgroup.gem mruby-cocoa.gem mruby-core-ext.gem mruby-curl.gem mruby-digest.gem mruby-dir.gem mruby-discount.gem mruby-dll.gem mruby-eject.gem mruby-env.gem mruby-fltk3.gem mruby-girffi.gem mruby-gles.gem mruby-gntp.gem mruby-growthforecast.gem mruby-hs-regexp.gem mruby-http.gem mruby-httprequest.gem mruby-iconv.gem mruby-io.gem mruby-javascriptcore.gem mruby-jpeg.gem mruby-json.gem mruby-libqrng.gem mruby-markdown.gem mruby-md5.gem mruby-mecab.gem mruby-msagent.gem mruby-mtest.gem mruby-mysql.gem mruby-oauth.gem mruby-onig-regexp.gem mruby-pack.gem mruby-pcre-regexp.gem mruby-pid.gem mruby-process.gem mruby-random.gem mruby-redis.gem mruby-require.gem mruby-sha1.gem mruby-simple-random.gem mruby-simplehttp.gem mruby-sinatic.gem mruby-sleep.gem mruby-sqlite3.gem mruby-string- enhancement.gem mruby-syslog.gem mruby-thread.gem mruby-updategems.gem mruby-uv.gem mruby-v8.gem mruby-winapp.gem mruby-wiringpi.gem mruby-zabbix.gem mgem-list Tuesday, May 14, 13
  8. Chunky Bacon git clone https://github.com/mruby/mruby.git cd mruby ruby minirake all

    test ./bin/mruby -e '2.times { p "Chunky Bacon!" }' Tuesday, May 14, 13
  9. Its all about extensions build_config.rb conf.gembox conf.gem default.gembox conf.gem Tuesday,

    May 14, 13
  10. mrbgems/default.gembox MRuby::GemBox.new do |conf| # Use standard Kernel#sprintf method conf.gem

    :core => "mruby-sprintf" # Use standard print/puts/p conf.gem :core => "mruby-print" # Use standard Math module conf.gem :core => "mruby-math" # Use standard Time class conf.gem :core => "mruby-time" # Use standard Struct class conf.gem :core => "mruby-struct" Let’s drop mruby-time Tuesday, May 14, 13
  11. Customize! ./bin/mruby -e “p Time.now” Comment out mruby-time in mrbgems/default.gembox

    ruby minirake clean all test ./bin/mruby -e “p Time.now” dont forget to uncomment mruby-time!! Tuesday, May 14, 13
  12. Your own mrbgem -1 CHALLENGE: make a rand/srand gem cd

    mrbgems mkdir mruby-rand mkdir mruby-rand/src mkdir mruby-rand/test vim mrbgems/mruby-rand/mrbgem.rake Tuesday, May 14, 13
  13. Your own mrbgem - 2 MRuby::Gem::Specification.new('mruby-rand') do |spec| spec.license =

    'MIT' spec.authors = 'mruby developers' end vim mrbgems/mruby-rand/mrbgem.rake Tuesday, May 14, 13
  14. Your own mrbgem - 3 #include “mruby.h” void mrb_mruby_rand_gem_init(mrb_state *mrb)

    { } void mrb_mruby_rand_gem_final(mrb_state *mrb) { } vim mrbgems/mruby-rand/src/rand.c Tuesday, May 14, 13
  15. Your own mrbgem - 4 MRuby::GemBox.new do |conf| # Use

    our massively awesome rand gem conf.gem :core => ‘mruby-rand’ end vim mrbgems/lrug.gembox # Use Random class # conf.gem :core => "mruby-random" vim mrbgems/default.gembox conf.gembox 'lrug' vim build_config.rb Tuesday, May 14, 13
  16. Your own mrbgem - 5 cd $(MRUBY_DIR) ruby minirake clean

    all ... Build summary: ================================================ Config Name: host Output Directory: build/host Binaries: mrbc Included Gems: mruby-rand - 0.0.0 mruby-sprintf - 0.0.0 mruby-print - 0.0.0 mruby-math - 0.0.0 mruby-time - 0.0.0 .... Tuesday, May 14, 13
  17. Your own mrbgem - 6 vim mrbgems/mruby-rand/test/rand.rb assert “rand is

    a Fixnum” do rand.class == Fixnum end assert “rand is positive” do rand >= 0 end ruby minirake clean all test Fail: rand is a Fixnum (mrbgems: mruby-rand) Tuesday, May 14, 13
  18. Your own mrbgem - 7 #include “mruby.h” #include “stdlib.h” mrb_value

    mrb_f_rand(mrb_state *mrb, mrb_value self) { return mrb_fixnum_value(rand()); } void mrb_mruby_rand_gem_init(mrb_state *mrb) { struct RClass *krn = mrb->kernel_module; mrb_define_method(mrb, krn, “rand”, mrb_f_rand, ARGS_ANY()); } ... vim mrbgems/mruby-rand/src/rand.c Tuesday, May 14, 13
  19. Your own mrbgem - 8 cd $(MRUBY_DIR) ruby minirake clean

    all test ./bin/mruby -e “p rand” ./bin/mruby -e “p rand, rand, rand” run that twice and... Tuesday, May 14, 13
  20. Your own mrbgem - 7 #include “mruby.h” #include “stdlib.h” #include

    “time.h” ... void mrb_mruby_rand_gem_init(mrb_state *mrb) { struct RClass *krn = mrb->kernel_module; mrb_define_method(mrb, krn, “rand”, mrb_f_rand, ARGS_ANY()); srand((unsigned) time(NULL)); } ... vim mrbgems/mruby-rand/src/rand.c Tuesday, May 14, 13
  21. Did I mention you can also write mrbgems in... Tuesday,

    May 14, 13
  22. Did I mention you can also Ruby write mrbgems in...

    Tuesday, May 14, 13
  23. Special Thanks @akiray03 - Akira Yumiyama jinbochorb https://github.com/jinbochorb Internet Initiative

    Japan mruby developers Tuesday, May 14, 13