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

mruby introduction -- jinbocho.rb #01

mruby introduction -- jinbocho.rb #01

16fc6001488a519a03dc4c0ca8eb4503?s=128

Akira Yumiyama

April 19, 2013
Tweet

Transcript

  1. mruby introduction mruby unofficial logo by https://github.com/h2so5/mruby-logo @akiray03 jinbocho.rb #01

  2. ࣗݾ঺հ @akiray03 ࠷ۙ࡞ͬͨ΋ͷ: mruby-mtest: MiniTestϥΠΫͳςετϑϨʔ ϜϫʔΫ mruby-WiringPi: Raspberry PiͷGPIOΛૢ࡞ MRuby

    Reporter: mrubyͷϏϧυ݁ՌΛू໿ http://m.ruby.iijgio.com/ 2
  3. Question? Rubyistͳํ? ૊ΈࠐΈܥͳํ? CݴޠόϦόϦॻ͍ͯΔํ? 3

  4. agenda mrubyͷ঺հ mrubyͰHello, world mrubyͷ֦ுϞδϡʔϧΛॻ͍ͯΈΑ͏ 4

  5. Hello, mruby git clone https://github.com/mruby/mruby.git cd mruby ruby minirake all

    test ./bin/mruby -e “Hello, mruby!” 5
  6. 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”?
  7. ܰྔRuby? ʮܰྔRubyʯ͸ϓϩδΣΫτ໊ ϓϩδΣΫτͷ੒Ռ෺(ιϑτ΢ΣΞ)͕ mruby ܦ࢈লͷʮ஍ҬΠϊϕʔγϣϯ૑ग़ݚڀ։ൃࣄۀʯʹ࠾୒͞Εͨ(2010ʙ2012೥) ܰྔRuby͸ʰ૊ΈࠐΈʱΛ೦಄ʹ։ൃ͞Ε͍ͯΔ 7

  8. ૊ΈࠐΈ? 8

  9. mruby target ػث૊ΈࠐΈ UI, Network, (ιϑτϦΞϧλΠϜͳ)੍ޚͳͲ ΞϓϦέʔγϣϯ૊ΈࠐΈ ίϯϑΟάهड़ݴޠ, ػೳ֦ு༻, Sandbox,

    ... 9
  10. mruby౥ࡌϧʔλ! ϧʔλ಺ͷίϯϑΟά؅ཧγεςϜͷಈ࡞ʹ mrubyΛ࠾༻ த਎͸ NetBSD ϧʔλͷ౷Ұ͞ΕͨίϯϑΟάΛ ֤Daemon༻ͷίϯϑΟάʹ ม׵͢Δ෦෼ͰmrubyΛ ར༻͍ͯ͠Δ 10

    ੈքॳ!
  11. Apache΁ͷ૊ΈࠐΈ mod_mruby Λ࢖͏͜ͱͰɺ Apache಺෦ͷঢ়ଶ Λ؆୯ʹ؂ࢹɾૢ࡞Ͱ͖Δ 11 http://blog.matsumoto-r.jp/?p=3376

  12. mrubyͷ༦΂ – http://www.slideshare.net/IIJ_MOGOK/mruby-17436495

  13. mrubyͷಛ௃ OS / CPUґଘ͕ແ͍ (গͳ͍) CRubyͷιʔείʔυಡΉͱifdefͩΒ͚... ػೳ͸CRubyͷαϒηοτ File, IO, Socket,

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

    ... ز͔ͭͷػೳ͸ࠩ͠ସ͕͑Ͱ͖Δ Regexp: pcre, onigruma, HenrySpencer, etc... 14
  15. mrbgems CRubyͷrubygemsͱࣅͨ࢓૊Έ CRuby͸ϥϯλΠϜ࣌ʹrequire͢Δ mruby͸Ϗϧυ࣌ʹ૊ࠐΉ఺͕ҟͳΔ mrubyʹ͸ (ඪ४Ͱ͸) requireແ͍ͷͰ.. ΫϩεϏϧυʹ΋ରԠ طʹ60Λ௒͑Δgem͕ϦϦʔε͞Ε͍ͯΔ 15

  16. 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
  17. 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
  18. small is beautiful 18 ΤίγεςϜ(mrbgem)͕ग़དྷͨ͜ͱͰɺࣗ෼ͷ ཉ͍͠ػೳ͚͕ͩೖͬͨʮΧελϜmrubyʯ͕؆ ୯ʹखʹೖΔΑ͏ʹͳͬͨ

  19. Hello, mruby! git clone https://github.com/mruby/mruby.git cd mruby ruby minirake all

    test ./bin/mruby -e “Hello, mruby!” 19
  20. 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͚ͩͰݸ
  21. 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ͳͲ
  22. 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
  23. Make mrbgem rand / srand Λ࡞ͬͯΈ·͠ΐ͏ mrbgems/mruby-random ͸ϝϧηϯψπΠε λͰ͕͢ɺ؆୯ʹCͷrandͱsrandΛWrapping͢ Δํ޲Ͱ࡞ͬͯΈ·͢

    αϯϓϧίʔυ͸ github: jinbochorb/mruby mruby-rand ϒϥϯνʹ͋Γ·͢ :p 23
  24. Make mrbgem – 1 cd $(MRUBY_DIR) mkdir mruby-rand mkdir mruby-rand/src

    mkdir mruby-rand/test 24
  25. 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
  26. 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
  27. 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(‘-’, ‘_’)ͨ͠΋ͷ
  28. 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
  29. Make mrbgem – 5 cd $(MRUBY_DIR) ruby minirake clean ruby

    minirake all (ͱΓ͋͑ͣϏϧυ͕௨Δ͜ͱ͚ͩ֬ೝ) 29
  30. 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
  31. Make mrbgem – 7 cd $(MRUBY_DIR) ruby minirake all test

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

    -e ‘p rand’ ./bin/mruby -e ‘p rand, rand, rand’ 33
  34. 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
  35. Make mrbgem 35 Rubyͷrand͸ɺCͷrandͱҧ͏ͱ͜Ζ͕͋Δ Ҿ਺ແ: [0,1) ͷൣғͰFloatͷཚ਺Λฦ͢ Ҿ਺༗: [0,max) ͷൣғͰFixnumͷཚ਺Λฦ͢

    Ұ౓΋srand͕ݺ͹Εͯͳ͚Ε͹srandΛݺͼग़͢ etc.. Λߟྀͯ͠࡞Δඞཁ͕͋Γ·͢ https://github.com/iij/mruby-simple-random/
  36. ·ͱΊ mruby ͸ configurable ͳ “͍ܰ” Ruby࣮૷ 2012೥य़ʹGithubͰެ։͞Εͨ mrbgemͳͲศརͳ࢓૊Έ΋ग़དྷͨ ػೳ֦ு(mrbgem)͸60௒

    “·ͩ҆ఆ͠ͳ͍͠”... ࢖Θͳ͍ͱ҆ఆ͠·ͤΜʂ օͰ࢖ͬͯ҆ఆͤ͞·͠ΐ͏ʂ 36
  37. FAQ

  38. FAQ Q. mruby͸ArduinoͰಈ͖·͔͢? Arduino DueͰ͸ಈ͍ͨͱ͍͏ใࠂ͕͋Γ·͢ http://blog.mruby.sh/201303161453.html mruby͸16bit؀ڥΛਖ਼ࣜαϙʔτ͍ͯ͠·ͤΜ ArduinoͰ࢖༻͞ΕΔAVR͸16bitϚΠίϯͰ͢ Arduinoϐϯޓ׵ͷchipKIT Max32,

    Enzi, GR- SAKURA, ౳Λར༻ͨ͠΄͏͕ݱ࣮తͰ͠ΐ͏ 38
  39. FAQ Q. mrubyͷ҆ఆ൛ϦϦʔε͸͍ͭͰ͔͢? A. ͍ͭͳΜͰ͠ΐ͏.... (׼ ҆ఆ൛͸NPOʹ೚ͤΔ (ҙ༁) github͸͜ͷ··ಥ͖ਐΉ (ҙ༁)

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

    ͱͯ͠ެ։͢Δͱɺ͍Ζ ͍Ζͳਓ͕تͿͱࢥ͍·͢ ཉ͍͠ͱᄁ͍ͨΒɺMͷ෇͘ਓ͕αΫοͱ࡞ͬͯ͘ ΕΔ͔΋͠Ε·ͤΜ :P 40
  41. FAQ Q. ͳΜͰmrubyΛ࢖ͬͨͷ? CRubyΛ࢖Θͳ͔ͬ ͨཧ༝͸? A. CRuby͸όΠφϦαΠζ/࣮ߦ࣌αΠζͱ΋ʹେ ͖͔ͬͨ CRuby͸ඪ४ϥΠϒϥϦɾϑϧηοτ͕લఏ Ұ෦෼ͷΈऔΓ֎͢ͷ͕೉͍͠/໘౗

    41