mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Update mruby.h.md
Added documentation of more of the C API functions found inside mruby.h
This commit is contained in:
@@ -67,3 +67,142 @@ char|mruby type|retrieve types|note
|
||||
`?`|optional given|`mrb_bool`|True if preceding argument is given. Used to check optional argument is given.
|
||||
|
||||
The passing variadic arguments must be a pointer of retrieving type.
|
||||
|
||||
### mrb_define_class
|
||||
```C
|
||||
MRB_API struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*);
|
||||
```
|
||||
Creates a new class. If you're creating a gem it may look something like this:
|
||||
|
||||
```C
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
struct RClass *example_class;
|
||||
example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
### mrb_define_method
|
||||
|
||||
```C
|
||||
MRB_API void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
|
||||
```
|
||||
|
||||
Creates a global function in ruby. If you're creating a gem it may look something like this:
|
||||
|
||||
```C
|
||||
mrb_value example_method(mrb_state* mrb, mrb_value self){
|
||||
puts("Executing example commad!");
|
||||
return self;
|
||||
}
|
||||
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
|
||||
Or maybe you want to create a class method for a class? It might look something like this:
|
||||
|
||||
```C
|
||||
mrb_value example_method(mrb_state* mrb, mrb_value self){
|
||||
puts("Examples are like pizza...");
|
||||
return self;
|
||||
}
|
||||
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
struct RClass *example_class;
|
||||
example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
|
||||
mrb_define_method(mrb, example_class, "example_method", example_method, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
### mrb_define_module
|
||||
|
||||
```C
|
||||
MRB_API struct RClass *mrb_define_module(mrb_state *, const char*);
|
||||
```
|
||||
|
||||
Creates a module in ruby. If you're creating a gem it may look something like this:
|
||||
|
||||
```C
|
||||
mrb_value example_method(mrb_state* mrb, mrb_value self){
|
||||
puts("Examples are like tacos...");
|
||||
return self;
|
||||
}
|
||||
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
struct RClass *example_module;
|
||||
example_module = mrb_define_module(mrb, "Example_Module");
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
|
||||
### mrb_define_module_function
|
||||
|
||||
```C
|
||||
MRB_API void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
|
||||
```
|
||||
|
||||
Defines a module function. If you're creating a gem it may look something like this:
|
||||
|
||||
|
||||
```C
|
||||
mrb_value example_method(mrb_state* mrb, mrb_value self){
|
||||
puts("Examples are like hot wings...");
|
||||
return self;
|
||||
}
|
||||
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
struct RClass *example_module;
|
||||
example_module = mrb_define_module(mrb, "Example_Module");
|
||||
mrb_define_module_function(mrb, example_module, "example_method", example_method, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
|
||||
### mrb_define_const
|
||||
|
||||
```C
|
||||
MRB_API void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
|
||||
```
|
||||
|
||||
Defines a constant. If you're creating a gem it may look something like this:
|
||||
|
||||
```C
|
||||
mrb_value example_method(mrb_state* mrb, mrb_value self){
|
||||
puts("Examples are like hot wings...");
|
||||
return self;
|
||||
}
|
||||
|
||||
void mrb_example_gem_init(mrb_state* mrb) {
|
||||
mrb_define_const(mrb, mrb->kernel_module, "EXAPMLE_CONSTANT", mrb_fixnum_value(0x00000001));
|
||||
}
|
||||
|
||||
void mrb_example_gem_final(mrb_state* mrb) {
|
||||
//free(TheAnimals);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user