on small devices
~ shashank daté ~
stdate@gmail.com
twitter: @shanko
github.com/shanko
1
minimalistic Ruby (mruby.org)
2
why?
3
raspberry pi (fun!)
Single Board Computer - Raspberry Pi Zero: ($5)
CPU: ARM 11, 1GHz - 32-bit single core
GPU: Broadcom VideoCore IV
RAM: 512MB LPDDR2 (900 MHz)
Networking: none Bluetooth: none
Storage: microSD
GPIO: 40-pin header, unpopulated
Ports: mini HDMI, micro USB 2.0,
4
raspberry pi (fun!)
Single Board Computer - Raspberry Pi 3: ($35)
CPU: ARM Cortex A53, 1.2 GHz, 64-bit quadcore
GPU: Broadcom VideoCore IV
RAM: 1 GB SDRAM (900 MHz)
Networking: 10/100 Mbits Ethernet, 802.11n Wifi
Storage: microSD
GPIO: 40-pin header, populated
Ports: HDMI, 4 x USB 2.0,
5
verifone (profit!)
Vx570 Processor
Memory
Display
Magnetic Card Reader
Keypad
I/O Modules
Peripheral Ports
Security
6
lightweight
7
size of the executable
8
minimal dependencies
9
mem @ runtime
10
install
pre-requisites:
11
installation
12
generates
binaries:
13
TIMTOWDI
14
embedding (ruby in c)
/* hello.c */
#include "mruby.h"
#include "mruby/compile.h"�int�main(void)�{� mrb_state *mrb = mrb_open();� if (!mrb) { printf(“handle error \n”); }� mrb_load_string(mrb, "puts 'hello world'");� mrb_close(mrb);�}
15
linking
$ gcc -std=c99 \ <<< compiler options
-I /path/to/include/mruby \ <<< include headers
hello.c \ <<< program file
-lm /path/to/build/host/lib/libmruby.a \ <<< linking
-o hello <<< executable
16
embedding (bytecode in c)
$ cat hello.rb
puts "hello world"
$ mrbc hello.rb # <<<< produces byte-coded hello.mrb
$ hexdump -c hello.mrb
0000000 R I T E 0 0 0 3 � � \0 \0 \0 e M A
0000010 T Z 0 0 0 0 I R E P \0 \0 \0 G 0 0
$ mruby -b hello.mrb
hello world
17
embedding (symbol table)
$ cat hello.rb
puts "hello world"
$ mrbc -B hw_symbol hello.rb
^^^^ produces symbol table in hello.c
$ cmp hello_world
^^^^ magic shell script to compile
$ ./hello_world
hello world
/* hello_world.c */
#include "mruby.h"
#include "mruby/irep.h"
#include "hello.c"
int main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) {/* handle error */}
mrb_load_irep(mrb, hw_symbol);
mrb_close(mrb);
}
18
magic shell script (cmp)
$ cat ~/bin/cmp
#!/bin/bash -x
gcc -std=c99 \
-I /$HOME/mruby/include $1.c \
$HOME/mruby/build/host/lib/libmruby.a \
-lm \
-o $1
19
customize mruby
mrbgems: library manager to integrate C and Ruby extensions
###### build_config.rb
conf.gem '/path/to/your/gem/dir'
conf.gem :git => 'https://github.com/masuidrive/mrbgems-example.git', :branch => 'master'�conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master'�conf.gem :bitbucket => 'mruby/mrbgems-example', :branch => 'master'
## from mgem-list use :mgem option
conf.gem :mgem => 'mruby-yaml'�conf.gem :mgem => 'json' # 'mruby-' prefix could be omitted
20
mruby vs. lua
Lua:
mRuby:
21
lua example
#include <stdio.h>�#include "lua.h"�#include "lualib.h"�#include "lauxlib.h"��/* the Lua interpreter */�lua_State* L;��int main ( int argc, char *argv[] )�{� /* initialize Lua */� L = lua_open();�� /* load Lua base libraries */� lua_baselibopen(L);�
� /* run the script */� lua_dofile(L, "hello.lua");�� /* cleanup Lua */� lua_close(L);�� return 0;�}
22
mruby vs. crystal
Crystal:
mRuby:
23
crystal example
require "http/client"
channel = Channel(String).new
spawn do
channel.send(HTTP::Client.get(
"https://crystal-lang.org/").body)
end
spawn do
channel.send(HTTP::Client.get(
"http://mruby.org").body)
end
2.times do
channel.receive
end
24
resources
mruby.org / lua.org / crystal-lang.org /
presentations by matz:
presentations by masayoshi takahashi & yurie yamane:
25
hack my pi
$ ssh pi@.....
Password: rubyconfindia
26