Commit e1b63837 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2826 from Mav7/master

Added some documentation for some of the functions found in array.h.
parents 630c2c58 0f93d9b7
......@@ -25,6 +25,6 @@ Header name|Features
[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](./value.h.md)|`mrb_value` functions and macros.
[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.
### 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]
```
......@@ -30,6 +30,7 @@ main(void)
mrb_funcall(mrb, obj, "method_name", 1, mrb_float_value(mrb, f));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
......@@ -77,6 +78,7 @@ main(void)
mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
......@@ -124,6 +126,7 @@ main(void)
mrb_funcall(mrb, obj, "method_name", 1, mrb_nil_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
......@@ -170,6 +173,7 @@ main(void)
mrb_funcall(mrb, obj, "method_name", 1, mrb_false_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
......@@ -217,6 +221,7 @@ main(void)
mrb_funcall(mrb, obj, "method_name", 1, mrb_true_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
......
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