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

mruby introduction -- jinbocho.rb #01

mruby introduction -- jinbocho.rb #01

Akira Yumiyama

April 19, 2013
Tweet

More Decks by Akira Yumiyama

Other Decks in Programming

Transcript

  1. mrubyͱ͸ Embeddable Minimal Implementation Considerable active committers have "M" as

    their name initials, somehow. Some major committers have Matsumoto as their family name, somehow. (They are not relatives) Matz. 6 What means “m”?
  2. mrubyͷಛ௃ OS / CPUґଘ͕ແ͍ (গͳ͍) CRubyͷιʔείʔυಡΉͱifdefͩΒ͚... ػೳ͸CRubyͷαϒηοτ File, IO, Socket,

    Thread, ... ͸ؚ·Εͳ͍ จ๏͸CRubyͷϑϧηοτ (༧ఆ) ISO / JIS ४ڌ (༧ఆ) 13
  3. configurable ෆཁͳػೳ͸ͦ͗མͱͤΔ Parser / Compiler p/puts/print, sprintf, Time, Math, Regexp,

    ... ز͔ͭͷػೳ͸ࠩ͠ସ͕͑Ͱ͖Δ Regexp: pcre, onigruma, HenrySpencer, etc... 14
  4. mgem list mruby-msagent mruby- capability mruby-WiringPi mruby-girffi mruby-md5 mruby-discount mruby-cocoa

    mruby-curl mruby-zabbix mruby-simple- random mruby-core-ext mruby-syslog mruby-process mruby-onig- regexp mruby-string- enhancement mruby-sinatic mruby-allegro mruby-sha1 mruby-sqlite3 mruby- javascriptcore mruby-dir mruby-fltk3 mruby-pack mruby-winapp mruby- httprequest mruby-oauth mruby- growthforecast mruby-audite mruby-cgroup mruby-thread mruby-libqrng mruby-jpeg mruby-random mruby-pcre- regexp mruby-mecab mruby-uv mruby-eject mruby-mysql mruby-hs-regexp mruby-redis mruby-http mruby-mtest mruby- updategems mruby-cfunc mruby-arduino mruby-require mruby-sleep mruby- simplehttp mruby-json mruby-base64 mruby-digest mruby-v8 mruby-env mruby-aws-s3 mruby-allocate mruby-gles mruby- markdown mruby-gntp mruby-dll mruby-iconv 16
  5. iij mrbgems mruby-mtest mruby-dir mruby-digest mruby-process mruby-pack mruby-syslog mruby-env mruby-simple-

    random mruby-errno mruby-aws-s3 mruby-io mruby-regexp-pcre mruby-tempfile mruby-require mruby-mdebug mruby-ipaddr Comming Soon 17
  6. build_config.rb .3VCZ#VJMEOFXEPcDPOGc UPPMDIBJOHDD DPOGHFN\SPPU^NSCHFNTNSVCZTQSJOUG DPOGHFN\SPPU^NSCHFNTNSVCZQSJOU DPOGHFN\SPPU^NSCHFNTNSVCZNBUI DPOGHFN\SPPU^NSCHFNTNSVCZUJNF DPOGHFN\SPPU^NSCHFNTNSVCZTUSVDU DPOGHFN\SPPU^NSCHFNTNSVCZFOVNFYU DPOGHFN\SPPU^NSCHFNTNSVCZTUSJOHFYU

    DPOGHFN\SPPU^NSCHFNTNSVCZOVNFSJDFYU DPOGHFN\SPPU^NSCHFNTNSVCZBSSBZFYU DPOGHFN\SPPU^NSCHFNTNSVCZIBTIFYU DPOGHFN\SPPU^NSCHFNTNSVCZSBOHFFYU DPOGHFN\SPPU^NSCHFNTNSVCZQSPDFYU DPOGHFN\SPPU^NSCHFNTNSVCZSBOEPN DPOGHFN\SPPU^NSCHFNTNSVCZCJONJSC DPOGHFN\SPPU^NSCHFNTNSVCZCJONSVCZ FOE 20 ຊՈNSVCZͰఏڙ͢Δ NSCHFN͚ͩͰݸ
  7. build_config.rb .3VCZ#VJMEOFXEPcDPOGc UPPMDIBJOHDD DPOGHFN\SPPU^NSCHFNTNSVCZTQSJOUG DPOGHFN\SPPU^NSCHFNTNSVCZQSJOU DPOGHFN\SPPU^NSCHFNTNSVCZNBUI DPOGHFN\SPPU^NSCHFNTNSVCZUJNF DPOGHFN\SPPU^NSCHFNTNSVCZTUSVDU DPOGHFN\SPPU^NSCHFNTNSVCZFOVNFYU DPOGHFN\SPPU^NSCHFNTNSVCZTUSJOHFYU

    DPOGHFN\SPPU^NSCHFNTNSVCZOVNFSJDFYU DPOGHFN\SPPU^NSCHFNTNSVCZBSSBZFYU DPOGHFN\SPPU^NSCHFNTNSVCZIBTIFYU DPOGHFN\SPPU^NSCHFNTNSVCZSBOHFFYU DPOGHFN\SPPU^NSCHFNTNSVCZQSPDFYU DPOGHFN\SPPU^NSCHFNTNSVCZSBOEPN DPOGHFN\SPPU^NSCHFNTNSVCZCJONJSC DPOGHFN\SPPU^NSCHFNTNSVCZCJONSVCZ FOE 21 toolchain :gcc conf.gem “#{root}/mrbgems/mruby-time” ଞʹ͸WTͳͲ
  8. Customize mruby ./bin/mruby -e ‘p Time.now’ build_config.rb ͷ mruby-time ͷߦΛίϝϯτΞ΢τ

    ruby minirake clean all test ΋͏Ұ౓ ./bin/mruby -e ‘p Time.now’ 22
  9. Make mrbgem – 2 25 MRuby::Gem::Specification.new('mruby-­‐rand')  do  |spec|    spec.license

     =  'MIT'    spec.authors  =  'mruby  developers' end vi mruby-rand/mrbgem.rake
  10. Make mrbgem – 3 26 #include  “mruby.h” void mrb_mruby_rand_gem_init(mrb_state  *mrb)

    { } void mrb_mruby_rand_gem_final(mrb_state  *mrb) { } vi mruby-rand/src/rand.c
  11. Make mrbgem – 3 27 #include  “mruby.h” void mrb_mruby_rand_gem_init(mrb_state  *mrb)

    { } void mrb_mruby_rand_gem_final(mrb_state  *mrb) { } vi mruby-rand/src/rand.c gemͷ໊લΛ gsub(‘-’, ‘_’)ͨ͠΋ͷ
  12. Make mrbgem – 4 28 MRuby::Build.new  do  |conf|    #

     ...    #  mruby-­‐random  ΛίϝϯτΞ΢τ͢Δ    #  conf.gem  “#{root}/mrbgems/mruby-­‐random”    #  mruby-­‐rand  Λ଍͢    conf.gem  “#{root}/mruby-­‐rand” end vi build_config.rb
  13. Make mrbgem – 5 cd $(MRUBY_DIR) ruby minirake clean ruby

    minirake all (ͱΓ͋͑ͣϏϧυ͕௨Δ͜ͱ͚ͩ֬ೝ) 29
  14. Make mrbgem – 6 30 assert  “rand  is  a  Fixnum”

     do    rand.class  ==  Fixnum end assert  “rand  is  a  positive  value”  do    rand  >=  0 end vi mruby-rand/test/rand.rb
  15. Make mrbgem – 7 cd $(MRUBY_DIR) ruby minirake all test

    (ࠓ౓͸ςετʹࣦഊ͍ͯ͠Δͱ͜Ζ·Ͱ֬ೝ) 31
  16. Make mrbgem – 8 32 #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()); } vi mruby-rand/src/rand.c
  17. Make mrbgem – 9 cd $(MRUBY_DIR) ruby minirake all ./bin/mruby

    -e ‘p rand’ ./bin/mruby -e ‘p rand, rand, rand’ 33
  18. Make mrbgem – 10 34 #include  <stdlib.h> #include  <time.h>  

               //  ←͜ͷߦΛ଍͢ void mrb_mruby_rand_gem_init(mrb_state  *mrb) {    //  (snip)...    srand((unsigned)  time(NULL));        //  ←͜ͷߦΛ଍͢ } vi mruby-rand/src/rand.c
  19. Make mrbgem 35 Rubyͷrand͸ɺCͷrandͱҧ͏ͱ͜Ζ͕͋Δ Ҿ਺ແ: [0,1) ͷൣғͰFloatͷཚ਺Λฦ͢ Ҿ਺༗: [0,max) ͷൣғͰFixnumͷཚ਺Λฦ͢

    Ұ౓΋srand͕ݺ͹Εͯͳ͚Ε͹srandΛݺͼग़͢ etc.. Λߟྀͯ͠࡞Δඞཁ͕͋Γ·͢ https://github.com/iij/mruby-simple-random/
  20. FAQ

  21. FAQ Q. mrubyͰ XXX(͓޷͖ͳΫϥε໊ΛೖΕͯͩ͘͞ ͍) ͸࢖͑·͔͢? A. Object.const_defined?(:XXX)ͯ͠Έ͍ͯͩ͘͞ ແ͚Ε͹࡞ͬͯ mrbgem

    ͱͯ͠ެ։͢Δͱɺ͍Ζ ͍Ζͳਓ͕تͿͱࢥ͍·͢ ཉ͍͠ͱᄁ͍ͨΒɺMͷ෇͘ਓ͕αΫοͱ࡞ͬͯ͘ ΕΔ͔΋͠Ε·ͤΜ :P 40