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. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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