-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.c
More file actions
83 lines (68 loc) · 2.19 KB
/
objects.c
File metadata and controls
83 lines (68 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/string.h>
#include <mruby/variable.h>
/* Equivalent to:
*
* class Human
* def initialize(name)
* @name = name
* end
* end
*/
static mrb_value mrb_human_new(mrb_state *mrb, mrb_value self) {
mrb_value name;
mrb_get_args(mrb, "S", &name);
// Set instance variable "@name" to mrb_value name
mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@name"), name);
return self;
}
/* This method returns a string greeting another human.
* Equivalent to:
*
* class Human
* # ...
* def greet(human)
* "Hello there #{human.name}"
* end
* end
*
* NOTE: since we're using the MRuby API there's no need
* for an attribute reader, so this is not exactly like the above
* ruby code!
*/
static mrb_value mrb_human_greet(mrb_state *mrb, mrb_value self) {
mrb_value otherHuman;
mrb_get_args(mrb, "o", &otherHuman);
// Our greeting will look as follows:
// 'Hello there #{other human name}'
//
// so, we'll need to know the other human's instance name
// Grab the other human's name
mrb_value otherName = mrb_iv_get(mrb, otherHuman, mrb_intern_lit(mrb, "@name"));
// We start building our greeting
mrb_value greeting = mrb_str_append(mrb, mrb_str_new_cstr(mrb, "Hello there "), otherName);
// Print the greeting
mrb_p(mrb, greeting);
return self;
}
int main(int argc, char *argv[]) {
mrb_state *mrb = mrb_open();
if (!mrb) {
fprintf(stderr, "Couldn't initialize MRuby\n");
return 1;
}
// We create our class, and define the methods for it
struct RClass *humanKlass = mrb_define_class(mrb, "Human", mrb->object_class);
mrb_define_method(mrb, humanKlass, "initialize", mrb_human_new, MRB_ARGS_REQ(1));
mrb_define_method(mrb, humanKlass, "greet", mrb_human_greet, MRB_ARGS_REQ(1));
// Create some humans and make them greet each other!
const char *code = "human1 = Human.new('John Doe')\n"
"human2 = Human.new('Jane Doe')\n"
"human1.greet(human2)\n"
"human2.greet(human1)\n";
mrb_load_string(mrb, code);
mrb_close(mrb);
return 0;
}