Commit 76f24d21 authored by Seba Gamboa's avatar Seba Gamboa

Testing yard generation

parent 2d29d140
......@@ -21,3 +21,5 @@ cscope.out
/bin
/build
/mruby-source-*.gem
doc
.yardoc
-
AUTHORS
MITL
CONTRIBUTING.md
guides/*.md
# C API Reference
This is a C API Reference.
The structure of this document will follow the directory structure of `include/` directory.
## Headers list
Header name|Features
-----------|--------
[mrbconf.h](../mrbconf/README.md)|Defines macros for mruby configurations.
[mruby.h](./mruby.h.md)|Main header of mruby C API. Include this first.
[mruby/array.h](./mruby/array.h.md)|`Array` class.
[mruby/class.h](./mruby/class.h.md)|`Class` class.
[mruby/compile.h](./mruby/compile.h.md)|mruby compiler.
[mruby/data.h](./mruby/data.h.md)|User defined object.
[mruby/debug.h](./mruby/debug.h.md)|Debugging.
[mruby/dump.h](./mruby/dump.h.md)|Dumping compiled mruby script.
[mruby/error.h](./mruby/error.h.md)|Error handling.
[mruby/gc.h](./mruby/gc.h.md)|Uncommon memory management stuffs.
[mruby/hash.h](./mruby/hash.h.md)|`Hash` class.
[mruby/irep.h](./mruby/irep.h.md)|Compiled mruby script.
[mruby/khash.h](./mruby/khash.h.md)|Defines of khash which is used in hash table of mruby.
[mruby/numeric.h](./mruby/numeric.h.md)|`Numeric` class and sub-classes of it.
[mruby/opode.h](./mruby/opcode.h.md)|Operation codes used in mruby VM.
[mruby/proc.h](./mruby/proc.h.md)|`Proc` class.
[mruby/range.h](./mruby/range.h.md)|`Range` class.
[mruby/re.h](./mruby/re.h.md)|`Regexp` class.
[mruby/string.h](./mruby/string.h.md)|`String` class.
[mruby/value.h](./mruby/value.h.md)|`mrb_value` functions and macros.
[mruby/variable.h](./mruby/variable.h.md)|Functions to access to mruby variables.
[mruby/version.h](./mruby/version.h.md)|Macros of mruby version.
# mruby.h
Basic header of mruby.
It includes **mrbconf.h**, **mruby/value.h**, **mruby/version.h** internally.
## `mrb_state` management
### mrb_open
```C
mrb_state* mrb_open();
```
Creates new `mrb_state`.
### mrb_allocf
```C
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void *ptr, size_t s, void *ud);
```
Function pointer type of custom allocator used in `mrb_open_allocf`.
The function pointing it must behave similarly as `realloc` except:
* If `ptr` is `NULL` it must allocate new space.
* If `s` is `NULL`, `ptr` must be freed.
### mrb_open_allocf
```C
mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
```
Create new `mrb_state` with custom allocator.
`ud` will be passed to custom allocator `f`.
If user data isn't required just pass `NULL`.
Function pointer `f` must satisfy requirements of its type.
### mrb_close
```C
void mrb_close(mrb_state *mrb);
```
Deletes `mrb_state`.
## Method
### mrb_get_args
```C
int mrb_get_args(mrb_state *mrb, const char *format, ...);
```
Retrieve arguments from `mrb_state`.
When applicable, implicit conversions (such as `to_str`,
`to_ary`, `to_hash`) are applied to received arguments.
Use it inside a function pointed by `mrb_func_t`.
It returns the number of arguments retrieved.
`format` is a list of following format specifiers:
char|mruby type|retrieve types|note
:---:|----------|--------------|---
`o`|`Object`|`mrb_value`|Could be used to retrieve any type of argument
`C`|`Class`/`Module`|`mrb_value`|
`S`|`String`|`mrb_value`|when ! follows, the value may be nil
`A`|`Array`|`mrb_value`|when ! follows, the value may be nil
`H`|`Hash`|`mrb_value`|when ! follows, the value may be nil
`s`|`String`|`char*`, `mrb_int`|Receive two arguments; s! gives (NULL,0) for nil
`z`|`String`|`char*`|NUL terminated string; z! gives NULL for nil
`a`|`Array`|`mrb_value*`, `mrb_int`|Receive two arguments; a! gives (NULL,0) for nil
`f`|`Float`|`mrb_float`|
`i`|`Integer`|`mrb_int`|
`b`|boolean|`mrb_bool`|
`n`|`Symbol`|`mrb_sym`|
`&`|block|`mrb_value`|
`*`|rest arguments|`mrb_value*`, `mrb_int`|Receive the rest of arguments as an array.
<code>&#124;</code>|optional||After this spec following specs would be optional.
`?`|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*);
```
Defines 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);
```
Defines 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 command!");
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*);
```
Defines a module. 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);
}
```
### mrb_str_new_cstr
```C
MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
```
Turns a C string into a Ruby string value.
### mrb_value mrb_funcall
```C
MRB_API mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, mrb_int,...);
```
Call existing ruby functions.
### mrb_ary_new
```C
mrb_value mrb_ary_new(mrb_state *mrb);
```
Initializes an array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we are declaring a variable new_ary of data type mrb_value. Then we are initializing it with the mrb_ary_new function which only takes an mruby state as an argument.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb);
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
### mrb_ary_push
```C
void mrb_ary_push(mrb_state*, mrb_value, mrb_value);
```
Pushes given value into an array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case after initializing our array. We are declaring two variables with the mrb_int data type random_value1 & random_value2 and we initialize them 70 and 60 respectively. Then we use the mrb_ary_push function to push values those values into the array.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60]
Array
```
## mrb_ary_pop
```C
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
```
Pops the last element from the array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case after initializing our array. We are declaring two variables with the mrb_int data type random_value1 & random_value2 and we initialize them 70 and 60 respectively. Then we use the mrb_ary_push function to push values those values into the array. Now here in the Ruby files we add another method
called pop_ary that will return the array alone(just to be clean) and you should see the last element gone.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
mrb_ary_pop(mrb, new_ary); // Pops the last element of the array. In this case 60.
mrb_funcall(mrb, obj, "pop_ary", 1, new_ary); // Calls the method again to show the results.
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
def pop_ary(a)
puts a
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60]
Array
[70]
```
## mrb_ary_ref
```C
mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
```
Returns a reference to an element of the array. Specified by the value given to mrb_int n.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we're declaring a variable ary_ref with the data type of mrb_value. Then we assign mrb_ary_ref to it getting new_ary's value at index 1.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value ary_ref; // Declare variable.
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
ary_ref = mrb_ary_ref(mrb, new_ary, 1); // Gets the value of new_ary's second element at index 1.
mrb_value obj = mrb_load_file(mrb,fp);
/* Passing the value from ary_ref to the method method_name.*/
mrb_funcall(mrb, obj, "method_name", 1, ary_ref);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
60
Fixnum
```
### mrb_ary_set
```C
void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
```
Sets a value to an index.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we're declaring a variable ary_ref with the data type of mrb_value. Then we assign mrb_ary_ref to it getting new_ary's value at index 1.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary;
mrb_value ary_obj;
mrb_int random_value1 = 70;
mrb_int random_value2 = 60;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb);
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
/* Sets the fixnum value of 7 to the second index of the array.*/
mrb_ary_set(mrb, new_ary, 2, mrb_fixnum_value(7));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "before_after", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
def before_after(a)
puts a
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60, 7]
```
### mrb_hash_new
```C
mrb_value mrb_hash_new(mrb_state *mrb);
```
Initializes a hash.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example initializes a hash. In pure Ruby doing this is equivalent
to Hash.new.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_hash);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
``` Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
### mrb_hash_set
```C
void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
```
Sets a keys and values to hashes.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example sets a key and value pair to a hash. In pure Ruby doing this is equivalent to:
```Ruby
a = {:da_key => 80}
```
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable.
mrb_sym hash_key = mrb_intern_cstr(mrb, "da_key"); // Declare a symbol.
mrb_int hash_value = 80; // Declare a fixnum value.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key), mrb_fixnum_value(hash_value)); // Set values to hash.
mrb_funcall(mrb, obj, "method_name", 1, new_hash);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
{:da_key=>80}
Hash
```
### mrb_hash_get
```C
mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
```
Gets a value from a key.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example gets a value from a key. In pure Ruby doing this is equivalent to:
```Ruby
a = {:da_key => 80}
a[:da_key]
```
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable for new hash object.
mrb_value get_hash_value; // Declare variable for getting a value from a hash.
mrb_sym hash_key_a = mrb_intern_cstr(mrb, "da_key1"); // Declare a symbol.
mrb_sym hash_key_b = mrb_intern_cstr(mrb, "da_key2"); // Declare a symbol.
mrb_int hash_value_a = 80; // Declare a fixnum value.
mrb_int hash_value_b = 90; // Declare a fixnum value.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_a), mrb_fixnum_value(hash_value_a)); // Set values to hash.
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_b), mrb_fixnum_value(hash_value_b)); // Set values to hash.
get_hash_value = mrb_hash_get(mrb, new_hash, mrb_symbol_value(hash_key_b)); // Get value from hash.
mrb_funcall(mrb, obj, "method_name", 1, get_hash_value);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
90
Fixnum
```
### mrb_hash_delete_key
```C
mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
```
Deletes hash key and value pair.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example deletes hash key and value pair. In pure Ruby doing this is equivalent to:
```Ruby
a = {:da_key1 => 80,:da_key2 => 90}
a.delete(:da_key2)
```
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable for new hash object.
mrb_value get_hash_value; // Declare variable for getting a value from a hash.
mrb_sym hash_key_a = mrb_intern_cstr(mrb, "da_key1"); // Declare a symbol.
mrb_sym hash_key_b = mrb_intern_cstr(mrb, "da_key2"); // Declare a symbol.
mrb_sym hash_key_b = mrb_intern_cstr(mrb, "da_key2"); // Declare a symbol.
mrb_int hash_value_a = 80; // Declare a fixnum value.
mrb_int hash_value_b = 90; // Declare a fixnum value.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_a), mrb_fixnum_value(hash_value_a)); // Set values to hash.
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_b), mrb_fixnum_value(hash_value_b)); // Set values to hash.
mrb_funcall(mrb, obj, "method_name", 1, new_hash);
mrb_hash_delete_key(mrb, new_hash, mrb_symbol_value(hash_key_b));
mrb_funcall(mrb, obj, "another_method_name", 1, new_hash);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
```Ruby
class Example_Class
def method_name(a)
puts "Hash pre deletion #{a}"
#puts a.class
end
# Show deleted key and value pair.
def another_method_name(a)
puts "Hash post deletion #{a}"
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
Hash pre deletion {:da_key1 => 80, :da_key2 => 90}
Hash post deletion {:da_key1 => 80}
```
### mrb_hash_keys
```C
mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash);
```
Gets an array of keys.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example gets an array of keys from a hash.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable for new hash object.
mrb_value get_hash_keys; // Declare variable for getting an array of keys.
mrb_sym hash_key_a = mrb_intern_cstr(mrb, "da_key1"); // Declare a symbol.
mrb_sym hash_key_b = mrb_intern_cstr(mrb, "da_key2"); // Declare a symbol.
mrb_int hash_value_a = 80; // Declare a fixnum value.
mrb_int hash_value_b = 90; // Declare a fixnum value.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_a), mrb_fixnum_value(hash_value_a)); // Set values to hash.
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_b), mrb_fixnum_value(hash_value_b)); // Set values to hash.
get_hash_keys = mrb_hash_keys(mrb, new_hash); // get an array of keys.
mrb_funcall(mrb, obj, "method_name", 1, get_hash_keys);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[:da_key1, :da_key2]
Array
```
### mrb_hash_clear
```C
mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
```
Clears the hash.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. This example clears the hash. In pure Ruby doing this is equivalent to:
```Ruby
a = {:da_key1 => 80,:da_key2 => 90}
a.clear
```
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/hash.h" // Needs the hash header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_value new_hash; // Declare variable for new hash object.
mrb_value get_hash; // Declare variable for getting a hash.
mrb_sym hash_key_a = mrb_intern_cstr(mrb, "da_key1"); // Declare a symbol.
mrb_sym hash_key_b = mrb_intern_cstr(mrb, "da_key2"); // Declare a symbol.
mrb_int hash_value_a = 80; // Declare a fixnum value.
mrb_int hash_value_b = 90; // Declare a fixnum value.
FILE *fp = fopen("test_ext.rb","r");
new_hash = mrb_hash_new(mrb); // Initialize hash.
mrb_value obj = mrb_load_file(mrb,fp);
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_a), mrb_fixnum_value(hash_value_a)); // Set values to hash.
mrb_hash_set(mrb, new_hash, mrb_symbol_value(hash_key_b), mrb_fixnum_value(hash_value_b)); // Set values to hash.
mrb_funcall(mrb, obj, "method_name", 1, new_hash);
get_hash = mrb_hash_clear(mrb, new_hash);
mrb_funcall(mrb, obj, "another_method_name", 1, get_hash);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
#### test_ext.rb
```Ruby
class Example_Class
def method_name(a)
puts "Hash pre clear #{a}"
#puts a.class
end
# Show clear hash.
def another_method_name(a)
puts "Hash post clear #{a}"
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
Hash pre clear {:da_key1 => 80, :da_key2 => 90}
Hash post clear {}
```
#### mrb_range_new
```C
mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, mrb_bool);
```
Initializes a Range. The first mrb_value being the beginning value and second being the ending value.
The third parameter is an mrb_bool value that represents the inclusion or exclusion of the last value.
If the third parameter is 0 then it includes the last value in the range. If the third parameter is 1
then it excludes the last value in the range.
C code
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/range.h" // Needs the range header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_int beg = 0;
mrb_int end = 2;
mrb_bool exclude = 1;
mrb_value range_obj;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
range_obj = mrb_range_new(mrb, mrb_fixnum_value(beg), mrb_fixnum_value(end), exclude);
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, range_obj);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
Ruby code
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
This returns the following:
```Ruby
0...2
Range
```
### Macros
#### REGEXP_CLASS
A string with the name of the REGEXP class.
## Macros
### mrb_str_ptr(s)
Returns a pointer from a Ruby string.
## Functions
### mrb_str_plus
```C
mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
```
Adds to strings together.
### mrb_ptr_to_str
```C
mrb_value mrb_ptr_to_str(mrb_state *, void*);
```
Converts pointer into a Ruby string.
### mrb_obj_as_string
```C
mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
```
Returns an object as a Ruby string.
### mrb_str_resize
```C
mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
```
Resizes the string's length.
### mrb_str_substr
```C
mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
```
Returns a sub string.
### mrb_string_type
```C
mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
```
Returns a Ruby string type.
### mrb_str_new_cstr
```C
const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
```
Returns a Ruby string as a C string.
### mrb_str_dup
```C
mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
```
Duplicates a string object.
### mrb_str_intern
```C
mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
```
Returns a symbol from a passed in string.
### mrb_str_to_str
```C
mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
```
Returns a converted string type.
### mrb_str_equal
```C
mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Returns true if the strings match and false if the strings don't match.
### mrb_str_cat
```C
mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
```
Returns a concated string comprised of a Ruby string and a C string.
### mrb_str_cat_cstr
```C
mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
```
Returns a concated string comprised of a Ruby string and a C string(A shorter alternative to mrb_str_cat).
### mrb_str_append
```C
mrb_value mrb_str_append(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Adds str2 to the end of str1.
### mrb_str_cmp
```C
int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Returns 0 if both Ruby strings are equal.
Returns a value < 0 if Ruby str1 is less than Ruby str2.
Returns a value > 0 if Ruby str2 is greater than Ruby str1.
### mrb_str_to_cstr
```C
char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
```
Returns a C string from a Ruby string.
### mrb_str_inspect
```C
mrb_str_inspect(mrb_state *mrb, mrb_value str);
```
Returns a printable version of str, surrounded by quote marks, with special characters escaped.
### mrb_float_value
```C
static inline mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
```
Returns a float in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in mrb_float f = 0.09. Alternatively
double i = 0.09 could also be used.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
#include "mruby/string.h"
int
main(void)
{
mrb_float f = 0.09;// or double i = 0.09;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_float_value(mrb, f));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_fixnum_value
```C
static inline mrb_value mrb_fixnum_value(mrb_int i)
```
Returns a fixnum in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in mrb_int i = 99. Alternativly int i = 99
could also be used.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_int i = 99; // or int i = 99;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_nil_value
```C
static inline mrb_value mrb_nil_value(void)
```
Returns nil in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get NillClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_nil_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_false_value
```C
static inline mrb_value mrb_false_value(void)
```
Returns false in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get FalseClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_false_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_true_value
```C
static inline mrb_value mrb_true_value(void)
```
Returns true in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get TrueClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_true_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### Macros
#### MRUBY_RUBY_VERSION
The version of Ruby used by mruby.
#### MRUBY_RUBY_ENGINE
Ruby engine.
#### MRUBY_VERSION
The mruby version.
#### MRUBY_RELEASE_MAJOR
Major release version.
#### MRUBY_RELEASE_MINOR
Minor release version.
#### MRUBY_RELEASE_NO
Release number.
#### MRUBY_RELEASE_DATE
Release date as a string.
#### MRUBY_RELEASE_YEAR
Release year.
#### MRUBY_RELEASE_MONTH
Release month.
#### MRUBY_RELEASE_DAY
Release day.
#### MRUBY_BIRTH_YEAR
The year mruby was first created.
#### MRUBY_AUTHOR
Mruby's authors.
#### MRB_STRINGIZE0(expr)
A passed in expression.
#### MRB_STRINGIZE(expr)
Passes in an expression to MRB_STRINGIZE0.
#### MRUBY_DESCRIPTION
mruby's version, and release date.
#### MRUBY_COPYRIGHT
mruby's copyright information.
# Core Classes
## Array
ISO Code | Mixins | Source File
--- | --- | ---
15.2.12 | n/a | src/array.c
### Class Methods
#### []
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.4.1 | src/array.c | mrb_ary_s_create
### Methods
#### *
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.2 | src/array.c | mrb_ary_times
#### +
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.1 | src/array.c | mrb_ary_plus
#### <<
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.3 | src/array.c | mrb_ary_push_m
#### []
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.4 | src/array.c | mrb_ary_aget
#### []=
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.5 | src/array.c | mrb_ary_aset
#### __ary_cmp
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/array.c | mrb_ary_cmp
#### __ary_eq
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/array.c | mrb_ary_eq
#### clear
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.6 | src/array.c | mrb_ary_clear
#### concat
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.8 | src/array.c | mrb_ary_concat_m
#### delete_at
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.9 | src/array.c | mrb_ary_delete_at
#### empty?
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.12 | src/array.c | mrb_ary_empty_p
#### first
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.13 | src/array.c | mrb_ary_first
#### index
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.14 | src/array.c | mrb_ary_index_m
#### initialize_copy
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.16 | src/array.c | mrb_ary_replace_m
#### join
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.17 | src/array.c | mrb_ary_join_m
#### last
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.18 | src/array.c | mrb_ary_last
#### length
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.19 | src/array.c | mrb_ary_size
#### pop
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.21 | src/array.c | mrb_ary_pop
#### push
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.22 | src/array.c | mrb_ary_push_m
#### replace
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.23 | src/array.c | mrb_ary_replace_m
#### reverse
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.24 | src/array.c | mrb_ary_reverse
#### reverse!
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.25 | src/array.c | mrb_ary_reverse_bang
#### rindex
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.26 | src/array.c | mrb_ary_rindex_m
#### shift
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.27 | src/array.c | mrb_ary_shift
#### size
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.28 | src/array.c | mrb_ary_size
#### slice
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.29 | src/array.c | mrb_ary_aget
#### unshift
ISO Code | Source File | C Function
--- | --- | ---
15.2.12.5.30 | src/array.c | mrb_ary_unshift_m
## Exception
ISO Code | Mixins | Source File
--- | --- | ---
15.2.22 | n/a | src/error.c
### Class Methods
#### exception
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/class.c | mrb_instance_new
### Methods
#### ==
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_equal
#### backtrace
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/backtrace.c | mrb_exc_backtrace
#### exception
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_exception
#### initialize
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_initialize
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_inspect
#### message
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_message
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/error.c | exc_to_s
## FalseClass
ISO Code | Mixins | Source File
--- | --- | ---
n/a | n/a | src/object.c
### Methods
#### &
ISO Code | Source File | C Function
--- | --- | ---
15.2.6.3.1 | src/object.c | false_and
#### ^
ISO Code | Source File | C Function
--- | --- | ---
15.2.6.3.2 | src/object.c | false_xor
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/object.c | false_to_s
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.6.3.3 | src/object.c | false_to_s
#### |
ISO Code | Source File | C Function
--- | --- | ---
15.2.6.3.4 | src/object.c | false_or
## Fixnum
ISO Code | Mixins | Source File
--- | --- | ---
n/a | n/a | src/numeric.c
### Methods
#### %
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.5 | src/numeric.c | fix_mod
#### &
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.9 | src/numeric.c | fix_and
#### *
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.3 | src/numeric.c | fix_mul
#### +
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.1 | src/numeric.c | fix_plus
#### -
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.2 | src/numeric.c | fix_minus
#### <<
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.12 | src/numeric.c | fix_lshift
#### ==
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.7 | src/numeric.c | fix_equal
#### >>
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.13 | src/numeric.c | fix_rshift
#### ^
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.11 | src/numeric.c | fix_xor
#### divmod
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.30 | src/numeric.c | fix_divmod
#### eql?
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.16 | src/numeric.c | fix_eql
#### hash
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.18 | src/numeric.c | flo_hash
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | fix_to_s
#### to_f
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.23 | src/numeric.c | fix_to_f
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.25 | src/numeric.c | fix_to_s
#### |
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.10 | src/numeric.c | fix_or
#### ~
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.8 | src/numeric.c | fix_rev
## Float
ISO Code | Mixins | Source File
--- | --- | ---
15.2.9 | n/a | src/numeric.c
### Methods
#### %
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.5 | src/numeric.c | flo_mod
#### *
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.3 | src/numeric.c | flo_mul
#### +
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.1 | src/numeric.c | flo_plus
#### -
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.2 | src/numeric.c | flo_minus
#### ==
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.7 | src/numeric.c | flo_eq
#### ceil
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.8 | src/numeric.c | flo_ceil
#### divmod
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | flo_divmod
#### eql?
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.16 | src/numeric.c | flo_eql
#### finite?
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.9 | src/numeric.c | flo_finite_p
#### floor
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.10 | src/numeric.c | flo_floor
#### infinite?
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.11 | src/numeric.c | flo_infinite_p
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | flo_to_s
#### nan?
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | flo_nan_p
#### round
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.12 | src/numeric.c | flo_round
#### to_f
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.13 | src/numeric.c | flo_to_f
#### to_i
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.14 | src/numeric.c | flo_truncate
#### to_int
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | flo_truncate
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.16 | src/numeric.c | flo_to_s
#### truncate
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.15 | src/numeric.c | flo_truncate
## Hash
ISO Code | Mixins | Source File
--- | --- | ---
15.2.13 | n/a | src/hash.c
### Methods
#### []
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.2 | src/hash.c | mrb_hash_aget
#### []=
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.3 | src/hash.c | mrb_hash_aset
#### __delete
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.8 | src/hash.c | mrb_hash_delete
#### clear
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.4 | src/hash.c | mrb_hash_clear
#### default
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.5 | src/hash.c | mrb_hash_default
#### default=
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.6 | src/hash.c | mrb_hash_set_default
#### default_proc
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.7 | src/hash.c | mrb_hash_default_proc
#### default_proc=
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.7 | src/hash.c | mrb_hash_set_default_proc
#### dup
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/hash.c | mrb_hash_dup
#### empty?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.12 | src/hash.c | mrb_hash_empty_p
#### has_key?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.13 | src/hash.c | mrb_hash_has_key
#### has_value?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.14 | src/hash.c | mrb_hash_has_value
#### include?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.15 | src/hash.c | mrb_hash_has_key
#### initialize
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.16 | src/hash.c | mrb_hash_init
#### key?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.18 | src/hash.c | mrb_hash_has_key
#### keys
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.19 | src/hash.c | mrb_hash_keys
#### length
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.20 | src/hash.c | mrb_hash_size_m
#### member?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.21 | src/hash.c | mrb_hash_has_key
#### shift
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.24 | src/hash.c | mrb_hash_shift
#### size
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.25 | src/hash.c | mrb_hash_size_m
#### store
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.26 | src/hash.c | mrb_hash_aset
#### to_hash
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.29 | src/hash.c | mrb_hash_to_hash
#### value?
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.27 | src/hash.c | mrb_hash_has_value
#### values
ISO Code | Source File | C Function
--- | --- | ---
15.2.13.4.28 | src/hash.c | mrb_hash_values
## Integer
ISO Code | Mixins | Source File
--- | --- | ---
15.2.8 | n/a | src/numeric.c
### Methods
#### to_i
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.24 | src/numeric.c | int_to_i
#### to_int
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | int_to_i
## NilClass
ISO Code | Mixins | Source File
--- | --- | ---
n/a | n/a | src/object.c
### Methods
#### &
ISO Code | Source File | C Function
--- | --- | ---
15.2.4.3.1 | src/object.c | false_and
#### ^
ISO Code | Source File | C Function
--- | --- | ---
15.2.4.3.2 | src/object.c | false_xor
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/object.c | nil_inspect
#### nil?
ISO Code | Source File | C Function
--- | --- | ---
15.2.4.3.4 | src/object.c | mrb_true
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.4.3.5 | src/object.c | nil_to_s
#### |
ISO Code | Source File | C Function
--- | --- | ---
15.2.4.3.3 | src/object.c | false_or
## Numeric
ISO Code | Mixins | Source File
--- | --- | ---
15.2.7 | n/a | src/numeric.c
### Methods
#### **
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/numeric.c | num_pow
#### /
ISO Code | Source File | C Function
--- | --- | ---
15.2.8.3.4 | src/numeric.c | num_div
#### <=>
ISO Code | Source File | C Function
--- | --- | ---
15.2.9.3.6 | src/numeric.c | num_cmp
#### quo
ISO Code | Source File | C Function
--- | --- | ---
15.2.7.4.5 | src/numeric.c | num_div
## Proc
ISO Code | Mixins | Source File
--- | --- | ---
15.2.17 | n/a | src/proc.c
### Methods
#### arity
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/proc.c | mrb_proc_arity
#### initialize
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/proc.c | mrb_proc_initialize
#### initialize_copy
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/proc.c | mrb_proc_init_copy
## Range
ISO Code | Mixins | Source File
--- | --- | ---
15.2.14 | n/a | src/range.c
### Methods
#### ==
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.1 | src/range.c | mrb_range_eq
#### ===
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.2 | src/range.c | mrb_range_include
#### begin
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.3 | src/range.c | mrb_range_beg
#### end
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.5 | src/range.c | mrb_range_end
#### eql?
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.14 | src/range.c | range_eql
#### exclude_end?
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.6 | src/range.c | mrb_range_excl
#### first
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.7 | src/range.c | mrb_range_beg
#### include?
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.8 | src/range.c | mrb_range_include
#### initialize
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.9 | src/range.c | mrb_range_initialize
#### initialize_copy
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.15 | src/range.c | range_initialize_copy
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.13 | src/range.c | range_inspect
#### last
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.10 | src/range.c | mrb_range_end
#### member?
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.11 | src/range.c | mrb_range_include
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.14.4.12 | src/range.c | range_to_s
## RuntimeError
ISO Code | Mixins | Source File
--- | --- | ---
15.2.28 | n/a | src/error.c
## ScriptError
ISO Code | Mixins | Source File
--- | --- | ---
15.2.37 | n/a | src/error.c
## StandardError
ISO Code | Mixins | Source File
--- | --- | ---
15.2.23 | n/a | src/error.c
## String
ISO Code | Mixins | Source File
--- | --- | ---
15.2.10 | n/a | src/string.c
### Methods
#### *
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.5 | src/string.c | mrb_str_times
#### +
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.4 | src/string.c | mrb_str_plus_m
#### <=>
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.1 | src/string.c | mrb_str_cmp_m
#### ==
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.2 | src/string.c | mrb_str_equal_m
#### []
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.6 | src/string.c | mrb_str_aref_m
#### bytes
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/string.c | mrb_str_bytes
#### bytesize
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/string.c | mrb_str_size
#### capitalize
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.7 | src/string.c | mrb_str_capitalize
#### capitalize!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.8 | src/string.c | mrb_str_capitalize_bang
#### chomp
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.9 | src/string.c | mrb_str_chomp
#### chomp!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.10 | src/string.c | mrb_str_chomp_bang
#### chop
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.11 | src/string.c | mrb_str_chop
#### chop!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.12 | src/string.c | mrb_str_chop_bang
#### downcase
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.13 | src/string.c | mrb_str_downcase
#### downcase!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.14 | src/string.c | mrb_str_downcase_bang
#### empty?
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.16 | src/string.c | mrb_str_empty_p
#### eql?
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.17 | src/string.c | mrb_str_eql
#### hash
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.20 | src/string.c | mrb_str_hash_m
#### include?
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.21 | src/string.c | mrb_str_include
#### index
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.22 | src/string.c | mrb_str_index_m
#### initialize
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.23 | src/string.c | mrb_str_init
#### initialize_copy
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.24 | src/string.c | mrb_str_replace
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.46 | src/string.c | mrb_str_inspect
#### intern
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.25 | src/string.c | mrb_str_intern
#### length
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.26 | src/string.c | mrb_str_size
#### replace
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.28 | src/string.c | mrb_str_replace
#### reverse
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.29 | src/string.c | mrb_str_reverse
#### reverse!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.30 | src/string.c | mrb_str_reverse_bang
#### rindex
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.31 | src/string.c | mrb_str_rindex_m
#### size
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.33 | src/string.c | mrb_str_size
#### slice
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.34 | src/string.c | mrb_str_aref_m
#### split
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.35 | src/string.c | mrb_str_split_m
#### to_f
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.38 | src/string.c | mrb_str_to_f
#### to_i
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.39 | src/string.c | mrb_str_to_i
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.40 | src/string.c | mrb_str_to_s
#### to_str
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/string.c | mrb_str_to_s
#### to_sym
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.41 | src/string.c | mrb_str_intern
#### upcase
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.42 | src/string.c | mrb_str_upcase
#### upcase!
ISO Code | Source File | C Function
--- | --- | ---
15.2.10.5.43 | src/string.c | mrb_str_upcase_bang
## Symbol
ISO Code | Mixins | Source File
--- | --- | ---
15.2.11 | n/a | src/symbol.c
### Methods
#### <=>
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/symbol.c | sym_cmp
#### ===
ISO Code | Source File | C Function
--- | --- | ---
15.2.11.3.1 | src/symbol.c | sym_equal
#### id2name
ISO Code | Source File | C Function
--- | --- | ---
15.2.11.3.2 | src/symbol.c | mrb_sym_to_s
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
15.2.11.3.5 | src/symbol.c | sym_inspect
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.11.3.3 | src/symbol.c | mrb_sym_to_s
#### to_sym
ISO Code | Source File | C Function
--- | --- | ---
15.2.11.3.4 | src/symbol.c | sym_to_sym
## SyntaxError
ISO Code | Mixins | Source File
--- | --- | ---
15.2.38 | n/a | src/error.c
## TrueClass
ISO Code | Mixins | Source File
--- | --- | ---
n/a | n/a | src/object.c
### Methods
#### &
ISO Code | Source File | C Function
--- | --- | ---
15.2.5.3.1 | src/object.c | true_and
#### ^
ISO Code | Source File | C Function
--- | --- | ---
15.2.5.3.2 | src/object.c | true_xor
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/object.c | true_to_s
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.2.5.3.3 | src/object.c | true_to_s
#### |
ISO Code | Source File | C Function
--- | --- | ---
15.2.5.3.4 | src/object.c | true_or
# Core Modules
## Comparable
ISO Code | Source File
--- | ---
15.3.3 | src/compar.c
## Enumerable
ISO Code | Source File
--- | ---
15.3.2 | src/enum.c
## GC
ISO Code | Source File
--- | ---
n/a | src/gc.c
### Class Methods
#### disable
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_disable
#### enable
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_enable
#### generational_mode
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_generational_mode_get
#### generational_mode=
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_generational_mode_set
#### interval_ratio
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_interval_ratio_get
#### interval_ratio=
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_interval_ratio_set
#### start
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_start
#### step_ratio
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_step_ratio_get
#### step_ratio=
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_step_ratio_set
#### test
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/gc.c | gc_test
## Kernel
ISO Code | Source File
--- | ---
15.3.1 | src/kernel.c
### Class Methods
#### block_given?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.2.2 | src/kernel.c | mrb_f_block_given_p_m
#### global_variables
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.2.4 | src/kernel.c | mrb_f_global_variables
#### iterator?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.2.5 | src/kernel.c | mrb_f_block_given_p_m
#### local_variables
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.2.7 | src/kernel.c | mrb_local_variables
#### raise
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.2.12 | src/kernel.c | mrb_f_raise
### Methods
#### !=
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/kernel.c | mrb_obj_not_equal_m
#### ==
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.1 | src/kernel.c | mrb_obj_equal_m
#### ===
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.2 | src/kernel.c | mrb_equal_m
#### __case_eqq
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/kernel.c | mrb_obj_ceqq
#### __id__
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.3 | src/kernel.c | mrb_obj_id_m
#### __send__
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.4 | src/kernel.c | mrb_f_send
#### block_given?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.6 | src/kernel.c | mrb_f_block_given_p_m
#### class
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.7 | src/kernel.c | mrb_obj_class_m
#### clone
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.8 | src/kernel.c | mrb_obj_clone
#### define_singleton_method
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/kernel.c | mod_define_singleton_method
#### dup
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.9 | src/kernel.c | mrb_obj_dup
#### eql?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.10 | src/kernel.c | mrb_obj_equal_m
#### equal?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.11 | src/kernel.c | mrb_obj_equal_m
#### extend
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.13 | src/kernel.c | mrb_obj_extend_m
#### global_variables
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.14 | src/kernel.c | mrb_f_global_variables
#### hash
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.15 | src/kernel.c | mrb_obj_hash
#### initialize_copy
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.16 | src/kernel.c | mrb_obj_init_copy
#### inspect
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.17 | src/kernel.c | mrb_obj_inspect
#### instance_eval
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.18 | src/kernel.c | mrb_obj_instance_eval
#### instance_of?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.19 | src/kernel.c | obj_is_instance_of
#### instance_variable_defined?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.20 | src/kernel.c | mrb_obj_ivar_defined
#### instance_variable_get
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.21 | src/kernel.c | mrb_obj_ivar_get
#### instance_variable_set
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.22 | src/kernel.c | mrb_obj_ivar_set
#### instance_variables
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.23 | src/kernel.c | mrb_obj_instance_variables
#### is_a?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.24 | src/kernel.c | mrb_obj_is_kind_of_m
#### iterator?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.25 | src/kernel.c | mrb_f_block_given_p_m
#### kind_of?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.26 | src/kernel.c | mrb_obj_is_kind_of_m
#### local_variables
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.28 | src/kernel.c | mrb_local_variables
#### methods
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.31 | src/kernel.c | mrb_obj_methods_m
#### nil?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.32 | src/kernel.c | mrb_false
#### object_id
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.33 | src/kernel.c | mrb_obj_id_m
#### private_methods
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.36 | src/kernel.c | mrb_obj_private_methods
#### protected_methods
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.37 | src/kernel.c | mrb_obj_protected_methods
#### public_methods
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.38 | src/kernel.c | mrb_obj_public_methods
#### raise
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.40 | src/kernel.c | mrb_f_raise
#### remove_instance_variable
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.41 | src/kernel.c | mrb_obj_remove_instance_variable
#### respond_to?
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.43 | src/kernel.c | obj_respond_to
#### send
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.44 | src/kernel.c | mrb_f_send
#### singleton_class
ISO Code | Source File | C Function
--- | --- | ---
n/a | src/kernel.c | mrb_singleton_class
#### singleton_methods
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.45 | src/kernel.c | mrb_obj_singleton_methods_m
#### to_s
ISO Code | Source File | C Function
--- | --- | ---
15.3.1.3.46 | src/kernel.c | mrb_any_to_s
# Language
mruby is an implementation of the Ruby programming language.
These documents are describing the language features and libraries
which are provided together with mruby.
## Built-In Class and Modules
see *doc/language/Core.md*
#!/usr/bin/env ruby
require 'pty'
c_dir = File.dirname(__FILE__)
MRUBY_ROOT = File.expand_path("#{c_dir}/../..")
DOC_DIR = File.expand_path(c_dir)
cmd = "ruby #{DOC_DIR}/mrbdoc/mrbdoc.rb #{MRUBY_ROOT} #{DOC_DIR} false"
IO.popen(cmd, "r+") do |io|
io.close_write
while line = io.gets
puts line
end
end
class MRBDoc
SRC_DIR = 'src'
MRBLIB_DIR = 'mrblib'
def analyze_code dir, &block
@mrb_files = {}
@dir = File.expand_path(dir)
block.call "MRBDOC\tanalyze #{@dir}"
analyze(dir) do |progress|
block.call progress
end
end
def each_file(&block); @mrb_files.each {|k,v| block.call k,v}; end
def find_c_func(c_func_name)
each_file do |file_name, file|
c_func = file.c_funcs(c_func_name)
return c_func unless c_func.nil?
end
{}
end
def find_c_file(rb_obj_name, c_func_name)
last_file_name_match = ''
each_file do |file_name, file|
c_func = file.c_funcs(c_func_name)
if c_func and file.rb_class(rb_obj_name) or file.rb_module(rb_obj_name)
return file_name
elsif c_func
last_file_name_match = file_name
end
end
last_file_name_match
end
def find_c_file_by_class(name)
each_file do |file_name, file|
rb_class = file.rb_class(name)
return file_name unless rb_class.nil?
end
'nil'
end
def find_c_file_by_module(name)
each_file do |file_name, file|
rb_module = file.rb_module(name)
return file_name unless rb_module.nil?
end
'nil'
end
private
def analyze dir, &block
collect_all_files dir, &block
end
def collect_all_files dir, &block
l = lambda {|f| block.call " - #{f.name}"}
collect_files(src_code_dir(dir), /\.c$/, &l)
collect_files(mrb_code_dir(dir), /\.rb$/, &l)
end
def collect_files dir, rxp, &block
Dir.foreach(dir) do |file|
next unless file =~ rxp
file_path = "#{dir}/#{file}"
mrb_file = MRBFile.new "#{file_path}"
@mrb_files["#{file_path}"] = mrb_file
block.call mrb_file
end
end
def src_code_dir dir; File.expand_path SRC_DIR, dir; end
def mrb_code_dir dir; File.expand_path MRBLIB_DIR, dir; end
end
class MRBFile
attr_reader :name
attr_reader :file
def initialize mrb_file
@file = mrb_file
@name = File.basename file
@c_funcs = {}
@rb_class_c_def = {}
@rb_method_c_def = {}
@rb_class_method_c_def = {}
@rb_module_c_def = {}
@last_line = nil
@assignments = {}
@assignments['mrb->object_class'] = 'Object'
@assignments['mrb->kernel_module'] = 'Kernel'
@assignments['mrb->module_class'] = 'Module'
@assignments['mrb->nil_class'] = 'NilClass'
@assignments['mrb->true_class'] = 'TrueClass'
@assignments['mrb->class_class'] = 'Class'
analyze
end
def each_class &block
@rb_class_c_def.each do |class_name, class_hsh|
block.call class_name, class_hsh
end
end
def each_method name, &block
@rb_method_c_def.each do |met_name, met_hsh|
met_name_tmp = met_name.sub /^#{name}_/, ''
block.call met_name_tmp, met_hsh if met_hsh[:rb_class] == name
end
end
def each_class_method name, &block
@rb_class_method_c_def.each do |met_name, met_hsh|
met_name_tmp = met_name.sub /^#{name}_/, ''
block.call met_name_tmp, met_hsh if met_hsh[:rb_class] == name
end
end
def each_module &block
@rb_module_c_def.each do |module_name, module_hsh|
block.call module_name, module_hsh
end
end
def each_core_object &block
each_class {|n| block.call n}
each_module {|n| block.call n}
end
def c_funcs c_func_name; @c_funcs[c_func_name]; end
def rb_class rb_class_name; @rb_class_c_def[rb_class_name]; end
def rb_module rb_module_name; @rb_module_c_def[rb_module_name]; end
private
def analyze
File.open(file).each_line.each_with_index do |line, idx|
line_no = idx.succ
if c_file?
analyze_c_line line, line_no
elsif rb_file?
analyze_rb_line line, line_no
else
raise ArgumentError.new "#{file} is a not supported file type"
end
@last_line = line.strip
end
end
def c_file?; (name =~ /\.c$/); end
def rb_file?; (name =~ /\.rb$/); end
RXP_C_VAR = /\s*([^\s]*?)\s*?/
RXP_C_STR = /\s*?\"(.*?)\"\s*?/
#RXP_C_ISO = /\s*\;\s*[\/\*]*\s*.*?([15\.]{0,3}[0-9\.]*)\s*[\\\\\*]*/
RXP_C_ISO = /\s*;\s*[\/\*]*[\sa-zA-Z]*([\d\.]*)[\sa-zA-Z]*[\*\/]*/
def analyze_c_line line, line_no
case line.strip
when /^([a-zA-Z\_][a-zA-Z\_0-9]*?)\((.*?)\)\s*?$/
# assuming c method definition
@c_funcs[$1] = {:line_no => line_no, :args => $2, :return => @last_line}
when /mrb_define_class\(.*?\,#{RXP_C_STR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby class definition in c
class_name = $1.clone
iso = $3.clone
iso.strip!
@rb_class_c_def[class_name] = {:c_object => $2, :iso => iso}
assigns = line.split '='
if assigns.size > 1
assigns[0..-2].each do |v|
@assignments[v.strip] = class_name
end
end
when /mrb_define_module\(.*?\,#{RXP_C_STR}\)#{RXP_C_ISO}/
# assuming ruby class definition in c
module_name = $1.clone
iso = $2.clone
iso.strip!
@rb_module_c_def[module_name] = {:iso => iso}
assigns = line.split '='
if assigns.size > 1
assigns[0..-2].each do |v|
@assignments[v.strip] = module_name
end
end
when /mrb_define_method\(.*?\,#{RXP_C_VAR}\,#{RXP_C_STR}\,#{RXP_C_VAR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby method definition in c
name = $1.clone
name = resolve_obj(name)
iso = $5.clone
iso.strip!
@rb_method_c_def["#{name}_#{$2}"] = {:c_func => $3, :args => $4, :rb_class => name, :iso => iso}
when /mrb_define_class_method\(.*?\,#{RXP_C_VAR}\,#{RXP_C_STR}\,#{RXP_C_VAR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby class method definition in c
class_name = $1.clone
class_name = resolve_obj(class_name)
iso = $5.clone
iso.strip!
@rb_class_method_c_def["#{class_name}_#{$2}"] = {:c_func => $3, :args => $4, :rb_class => class_name, :iso => iso}
when /mrb_name_class\(.*?\,#{RXP_C_VAR}\,\s*mrb_intern\(.*?,#{RXP_C_STR}\)\)#{RXP_C_ISO}/
class_name = $2.clone
iso = $3.clone
iso.strip!
@rb_class_c_def[class_name] = {:c_object => $1, :iso => iso}
@assignments[$1] = class_name
when /mrb_include_module\(.*?\,#{RXP_C_VAR}\,\s*mrb_class_get\(.*?\,#{RXP_C_STR}\)\)/
class_name = resolve_obj($1)
mod = $2.clone
@rb_class_c_def[class_name][:include] = [] unless @rb_class_c_def[class_name].has_key? :include
@rb_class_c_def[class_name][:include] << mod
end
end
def analyze_rb_line line, line_no
end
def resolve_obj c_var
@assignments[c_var]
end
end
class MRBDoc
def write_documentation dir, cfg, &block
block.call "MRBDOC\twrite to #{File.expand_path(dir)}"
write(dir, cfg) do |progress|
block.call progress
end
end
private
def write dir, cfg
File.open(File.expand_path('Core.md', dir), 'wb+') do |io|
print_core_classes(io, cfg)
print_core_modules(io, cfg)
end
end
def get_core_list id
core_list = {}
each_file do |file_path, mrb_file|
mrb_file.send(id) do |name, cls_hsh|
core_list[name] = {:data => cls_hsh, :methods => {}, :class_methods => {}}
mrb_file.each_method name do |met_name, met_hsh|
core_list[name][:methods][met_name] = met_hsh
end
mrb_file.each_class_method name do |met_name, met_hsh|
core_list[name][:class_methods][met_name] = met_hsh
end
end
end
core_list
end
def print_core_classes(io, cfg)
core_list = get_core_list :each_class
io.puts "# Core Classes\n\n"
core_list.sort.each do |name, hsh|
file = find_c_file_by_class(name)
file = file.split("#{@dir}/")[1]
iso = hsh[:data][:iso]
iso = 'n/a' if iso.nil? or iso == ''
mixins = hsh[:data][:include].join(', ') unless hsh[:data][:include].nil?
mixins = 'n/a' if mixins.nil? or mixins == ''
io.puts <<CLASS
## #{name}
ISO Code | Mixins | Source File
--- | --- | ---
#{iso} | #{mixins} | #{file}
CLASS
print_class_methods(io, hsh, cfg)
print_methods(io, hsh, cfg)
end
end
def print_core_modules(io, cfg)
core_list = get_core_list :each_module
io.puts "# Core Modules\n\n"
core_list.sort.each do |name, hsh|
file = find_c_file_by_module(name)
file = file.split("#{@dir}/")[1]
iso = hsh[:data][:iso]
iso = 'n/a' if iso.nil? or iso == ''
io.puts <<CLASS
## #{name}
ISO Code | Source File
--- | ---
#{iso} | #{file}
CLASS
print_class_methods(io, hsh, cfg)
print_methods(io, hsh, cfg)
end
end
def print_methods(io, hsh, cfg)
return unless hsh[:methods].size > 0
io.puts "### Methods\n\n"
hsh[:methods].sort.each do |met_name, met_hsh|
print_method(io, met_name, met_hsh, cfg)
end
end
def print_class_methods(io, hsh, cfg)
return unless hsh[:class_methods].size > 0
io.puts "### Class Methods\n\n"
hsh[:class_methods].sort.each do |met_name, met_hsh|
print_method(io, met_name, met_hsh, cfg)
end
end
def print_method(io, met_name, met_hsh, cfg)
if cfg[:print_line_no]
line_no_head = ' | Line'
line_no = " | #{find_c_func(met_hsh[:c_func])[:line_no]}"
else
line_no, line_no_head = '', ''
end
file = find_c_file(met_hsh[:rb_class], met_hsh[:c_func])
file = file.split("#{@dir}/")[1]
iso = met_hsh[:iso]
iso = 'n/a' if iso.nil? or iso == ''
io.puts <<METHOD
#### #{met_name}
ISO Code | Source File | C Function#{line_no_head}
--- | --- | ---
#{iso} | #{file} | #{met_hsh[:c_func]}#{line_no}
METHOD
end
end
#!/usr/bin/env ruby
$: << File.dirname(__FILE__) + '/lib'
require 'mrbdoc_analyze'
require 'mrbdoc_docu'
MRUBY_ROOT = ARGV[0]
DOC_ROOT = ARGV[1]
_WRITE_LINE_NO = ARGV[2]
STDOUT.sync = true
raise ArgumentError.new 'mruby root missing!' if MRUBY_ROOT.nil?
raise ArgumentError.new 'doc root missing!' if DOC_ROOT.nil?
if _WRITE_LINE_NO.nil?
WRITE_LINE_NO = true
else
case _WRITE_LINE_NO
when 'true'
WRITE_LINE_NO = true
when 'false'
WRITE_LINE_NO = false
else
raise ArgumentError.new 'Line no parameter has to be false or true!'
end
end
mrbdoc = MRBDoc.new
mrbdoc.analyze_code MRUBY_ROOT do |progress|
puts progress
end
cfg = {:print_line_no => WRITE_LINE_NO}
mrbdoc.write_documentation DOC_ROOT, cfg do |progress|
puts progress
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment