diff options
Diffstat (limited to 'doc/api')
| -rw-r--r-- | doc/api/array.h.md | 251 | ||||
| -rw-r--r-- | doc/api/mruby.h.md | 148 | ||||
| -rw-r--r-- | doc/api/value.h.md | 238 |
3 files changed, 636 insertions, 1 deletions
diff --git a/doc/api/array.h.md b/doc/api/array.h.md new file mode 100644 index 000000000..e1fb0003d --- /dev/null +++ b/doc/api/array.h.md @@ -0,0 +1,251 @@ +### 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]
+```
diff --git a/doc/api/mruby.h.md b/doc/api/mruby.h.md index 5df0b6f04..8862fee2c 100644 --- a/doc/api/mruby.h.md +++ b/doc/api/mruby.h.md @@ -49,7 +49,7 @@ It returns number of function retrieved. char|mruby type|retrieve types|note :---:|----------|--------------|--- -`o`|`Object`|`mrb_value`|Could be used to retreive any type of argument +`o`|`Object`|`mrb_value`|Could be used to retrieve any type of argument `C`|`Class`/`Module`|`mrb_value`| `S`|`String`|`mrb_value`| `A`|`Array`|`mrb_value`| @@ -67,3 +67,149 @@ 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*); +``` +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. diff --git a/doc/api/value.h.md b/doc/api/value.h.md new file mode 100644 index 000000000..f3ae2d421 --- /dev/null +++ b/doc/api/value.h.md @@ -0,0 +1,238 @@ +### 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
+```
|
