You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/articles/executing-ruby-code-with-mruby.md
+44-21Lines changed: 44 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,12 @@ categories: articles
8
8
9
9
**originally written by [Daniel Bovensiepen](http://bovensiepen.net/)**
10
10
11
-
The traditional way to execute Ruby code is distributing the plain source code and requiring a Ruby interpreter with lots of files to be installed. mruby can be used like that too. So if you have experience with another Ruby implementation you may know some of the examples listed here. However with mruby you can also build standalone programs and compileto bytecode which can be dumped and loaded.
11
+
The traditional way to execute Ruby code is distributing the plain source code
12
+
and requiring a Ruby interpreter with lots of files to be installed.
13
+
mruby can be used like that too. So if you have experience with
14
+
another Ruby implementation you may know some of the examples listed here.
15
+
However with mruby you can also build standalone programs and compile to
16
+
bytecode which can be dumped and loaded.
12
17
13
18
A file called `test_program.rb` is used in some of the examples. Its content is:
14
19
@@ -18,7 +23,8 @@ puts 'hello world'
18
23
19
24
## REPL (mirb)
20
25
21
-
Not exactly directly used for program execution but still, Ruby code can be evaluated by using the `mirb` program:
26
+
Not exactly directly used for program execution but still,
27
+
Ruby code can be evaluated by using the `mirb` program:
22
28
23
29
~~~
24
30
$ mruby/bin/mirb
@@ -35,11 +41,13 @@ hello world
35
41
36
42
✘ not usable for productive execution
37
43
38
-
✘ the input needs to be parsed twice: first `mirb` checks if the code is complete and afterwards `mirb` compiles it into bytecode and executes it
44
+
✘ the input needs to be parsed twice: first `mirb` checks if the code is
45
+
complete and afterwards `mirb` compiles it into bytecode and executes it
39
46
40
47
## Source code (.rb)
41
48
42
-
The probably most common way to run Ruby code is by passing a filename as argument to an interpreter. In mruby that's the `mruby` program:
49
+
The probably most common way to run Ruby code is by passing a filename as
50
+
argument to an interpreter. In mruby that's the `mruby` program:
43
51
44
52
~~~
45
53
$ mruby/bin/mruby test_program.rb
@@ -58,7 +66,8 @@ hello world
58
66
59
67
## Source code (.c)
60
68
61
-
Ruby code can also be written as a C string. This is similar to the `-e` switch of the `mruby` program.
69
+
Ruby code can also be written as a C string. This is similar to
70
+
the `-e` switch of the `mruby` program.
62
71
63
72
~~~c
64
73
#include"mruby.h"
@@ -69,7 +78,7 @@ main(void)
69
78
{
70
79
mrb_state *mrb = mrb_open();
71
80
if (!mrb) { /* handle error */ }
72
-
// mrb_load_nstring() for strings without null terminator or known length
81
+
// mrb_load_nstring() for strings without null terminator or with known length
✘ additional boilerplate is needed to get the program up and running
98
109
99
110
✘ Ruby code has to be parsed and compiled to bytecode before its execution
100
111
101
-
✘ updating the Ruby code might require a recompilation of the C code or an advanced updating mechanism
112
+
✘ updating the Ruby code might require a recompilation of the C code or
113
+
an advanced updating mechanism
102
114
103
115
## Bytecode (.mrb)
104
116
105
-
mruby provides a Java-like execution style by compiling to an intermediate representation form which then will be executed.
117
+
mruby provides a Java-like execution style by compiling to an
118
+
intermediate representation form which then will be executed.
106
119
107
120
The first step is to compile source code to bytecode with the `mrbc` program:
108
121
109
122
~~~
110
123
$ mruby/bin/mrbc test_program.rb
111
124
~~~
112
125
113
-
This will produce a file called `test_program.mrb` which contains the intermediate representation of the previously given Ruby code:
126
+
This will produce a file called `test_program.mrb` which contains the
127
+
intermediate representation of the previously given Ruby code:
114
128
115
129
~~~
116
130
$ hexdump -C test_program.mrb
@@ -124,7 +138,9 @@ $ hexdump -C test_program.mrb
124
138
00000065
125
139
~~~
126
140
127
-
This file can be executed by the `mruby` program or the `mrb_load_irep_file()` function. The `-b` switch tells the program that the file is binary rather than plain Ruby code:
141
+
This file can be executed by the `mruby` program or the `mrb_load_irep_file()`
142
+
function. The `-b` switch tells the program that the file is binary rather than
143
+
plain Ruby code:
128
144
129
145
~~~
130
146
$ mruby/bin/mruby -b test_program.mrb
@@ -139,21 +155,25 @@ hello world
139
155
140
156
✔ bytecode can easily be updated by replacing the file
✘ the `mruby` program and a file system is required
145
162
146
163
## Bytecode (.c)
147
164
148
-
This variant is interesting for those who want to integrate Ruby code directly into their C code. It will create a C array containg the bytecode which you then have to execute by yourself.
165
+
This variant is interesting for those who want to integrate Ruby code directly
166
+
into their C code. It will create a C array containg the bytecode which you
167
+
then have to execute by yourself.
149
168
150
-
The first step is to compile the Ruby program. This is done by using `mrbc` and its `-B` switch. An identifier for the array also has to be given:
169
+
The first step is to compile the Ruby program. This is done by using `mrbc`
170
+
and its `-B` switch. An identifier for the array also has to be given:
151
171
152
172
~~~
153
173
$ mruby/bin/mrbc -Btest_symbol test_program.rb
154
174
~~~
155
175
156
-
This will create a C file called `test_program.c` containing the `test_symbol` array:
176
+
This creates a file called `test_program.c` containing the `test_symbol` array:
157
177
158
178
~~~c
159
179
/* dumped in little endian order.
@@ -176,7 +196,8 @@ test_symbol[] = {
176
196
};
177
197
~~~
178
198
179
-
To execute this bytecode the following boilerplate has to be written. It reads the array and executes the bytecode immediately:
199
+
To execute this bytecode the following boilerplate has to be written.
200
+
It reads the array and executes the bytecode immediately:
Copy file name to clipboardExpand all lines: docs/index.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,11 @@ title: mruby - docs
5
5
6
6
# Documentation
7
7
8
-
At the moment the source code ([`mruby/doc/`](https://github.com/mruby/mruby/tree/master/doc)) includes further documentation about compiling, configurating (`mrbconf`), debugging (`mrdb`), and extending (`mrbgems`) mruby.
8
+
At the moment the source code ([`mruby/doc/`][mruby-doc]) includes further
9
+
documentation about compiling, configurating (`mrbconf`), debugging (`mrdb`),
0 commit comments