From 7284d1d295c0eaaa2e36ce52e9dd65f8221ed8b2 Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Sun, 31 May 2015 23:26:33 -0400 Subject: Update mruby.h.md Added documentation of more of the C API functions found inside mruby.h --- doc/api/mruby.h.md | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) (limited to 'doc/api') diff --git a/doc/api/mruby.h.md b/doc/api/mruby.h.md index f537b6482..36712e5dc 100644 --- a/doc/api/mruby.h.md +++ b/doc/api/mruby.h.md @@ -67,3 +67,142 @@ char|mruby type|retrieve types|note `?`|optional given|`mrb_bool`|True if preceding argument is given. Used to check optional argument is given. The passing variadic arguments must be a pointer of retrieving type. + +### mrb_define_class +```C +MRB_API struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*); +``` +Creates a new class. If you're creating a gem it may look something like this: + +```C +void mrb_example_gem_init(mrb_state* mrb) { + struct RClass *example_class; + example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` +### mrb_define_method + +```C +MRB_API void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec); +``` + +Creates a global function in ruby. If you're creating a gem it may look something like this: + +```C +mrb_value example_method(mrb_state* mrb, mrb_value self){ + puts("Executing example commad!"); + return self; +} + +void mrb_example_gem_init(mrb_state* mrb) { + mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE()); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` + +Or maybe you want to create a class method for a class? It might look something like this: + +```C +mrb_value example_method(mrb_state* mrb, mrb_value self){ + puts("Examples are like pizza..."); + return self; +} + +void mrb_example_gem_init(mrb_state* mrb) { + struct RClass *example_class; + example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class); + mrb_define_method(mrb, example_class, "example_method", example_method, MRB_ARGS_NONE()); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` +### mrb_define_module + +```C +MRB_API struct RClass *mrb_define_module(mrb_state *, const char*); +``` + +Creates a module in ruby. If you're creating a gem it may look something like this: + +```C +mrb_value example_method(mrb_state* mrb, mrb_value self){ + puts("Examples are like tacos..."); + return self; +} + +void mrb_example_gem_init(mrb_state* mrb) { + struct RClass *example_module; + example_module = mrb_define_module(mrb, "Example_Module"); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` + +### mrb_define_module_function + +```C +MRB_API void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec); +``` + +Defines a module function. If you're creating a gem it may look something like this: + + +```C +mrb_value example_method(mrb_state* mrb, mrb_value self){ + puts("Examples are like hot wings..."); + return self; +} + +void mrb_example_gem_init(mrb_state* mrb) { + struct RClass *example_module; + example_module = mrb_define_module(mrb, "Example_Module"); + mrb_define_module_function(mrb, example_module, "example_method", example_method, MRB_ARGS_NONE()); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` + +### mrb_define_const + +```C +MRB_API void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value); +``` + +Defines a constant. If you're creating a gem it may look something like this: + +```C +mrb_value example_method(mrb_state* mrb, mrb_value self){ + puts("Examples are like hot wings..."); + return self; +} + +void mrb_example_gem_init(mrb_state* mrb) { + mrb_define_const(mrb, mrb->kernel_module, "EXAPMLE_CONSTANT", mrb_fixnum_value(0x00000001)); +} + +void mrb_example_gem_final(mrb_state* mrb) { + //free(TheAnimals); +} +``` + + + + + + + + + -- cgit v1.2.3 From 4959e4958abca0404c60fe54a4c700bfd1a25eb6 Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Sun, 31 May 2015 23:27:12 -0400 Subject: Update mruby.h.md --- doc/api/mruby.h.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc/api') diff --git a/doc/api/mruby.h.md b/doc/api/mruby.h.md index 36712e5dc..88fa2c7e5 100644 --- a/doc/api/mruby.h.md +++ b/doc/api/mruby.h.md @@ -94,7 +94,7 @@ Creates a global function in ruby. If you're creating a gem it may look somethin ```C mrb_value example_method(mrb_state* mrb, mrb_value self){ - puts("Executing example commad!"); + puts("Executing example command!"); return self; } -- cgit v1.2.3 From d90ccd7e6d06582ddb3d3c02e8337551a35b67d8 Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Sun, 31 May 2015 23:33:36 -0400 Subject: Update mruby.h.md --- doc/api/mruby.h.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'doc/api') diff --git a/doc/api/mruby.h.md b/doc/api/mruby.h.md index 88fa2c7e5..5a71f5cd4 100644 --- a/doc/api/mruby.h.md +++ b/doc/api/mruby.h.md @@ -72,7 +72,7 @@ The passing variadic arguments must be a pointer of retrieving type. ```C MRB_API struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*); ``` -Creates a new class. If you're creating a gem it may look something like this: +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) { @@ -90,7 +90,7 @@ void mrb_example_gem_final(mrb_state* mrb) { MRB_API void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec); ``` -Creates a global function in ruby. If you're creating a gem it may look something like this: +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){ @@ -131,7 +131,7 @@ void mrb_example_gem_final(mrb_state* mrb) { MRB_API struct RClass *mrb_define_module(mrb_state *, const char*); ``` -Creates a module in ruby. If you're creating a gem it may look something like this: +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){ @@ -198,11 +198,12 @@ void mrb_example_gem_final(mrb_state* mrb) { } ``` +### 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. -- cgit v1.2.3 From 1400c6e88ba20a07980770aa9217387a2cbd89bd Mon Sep 17 00:00:00 2001 From: Mav7 Date: Sun, 31 May 2015 23:50:56 -0400 Subject: Added a .md file for value.h --- doc/api/value.h.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 doc/api/value.h.md (limited to 'doc/api') diff --git a/doc/api/value.h.md b/doc/api/value.h.md new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3 From 297b6fe6e6f97bc8e4ca95d47fa6b02f7eab6396 Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Mon, 1 Jun 2015 00:42:23 -0400 Subject: Update value.h.md Wrote documentation for functions found in the value.h file. --- doc/api/value.h.md | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) (limited to 'doc/api') diff --git a/doc/api/value.h.md b/doc/api/value.h.md index e69de29bb..dd1c4b238 100644 --- a/doc/api/value.h.md +++ b/doc/api/value.h.md @@ -0,0 +1,233 @@ +### 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 +#include +#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); +} + +``` + +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 +#include +#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); +} + +``` + +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 +#include +#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); +} + +``` + +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 +#include +#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); +} + +``` + +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 +#include +#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); +} + +``` + +test.rb +```Ruby +class My_Class + def method_name(s) + puts s + puts s.class + end +end +a = My_Class.new +``` -- cgit v1.2.3 From 1d90018cfa93e63366f4f1fe600bb356dabf8c3b Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Mon, 1 Jun 2015 00:44:34 -0400 Subject: Update mruby.h.md --- doc/api/mruby.h.md | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'doc/api') diff --git a/doc/api/mruby.h.md b/doc/api/mruby.h.md index 5a71f5cd4..8862fee2c 100644 --- a/doc/api/mruby.h.md +++ b/doc/api/mruby.h.md @@ -207,3 +207,9 @@ 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. -- cgit v1.2.3 From f8b1de0ab47f3f0bae861bf11f73635aa8c02ba6 Mon Sep 17 00:00:00 2001 From: Ralph Desir Date: Mon, 1 Jun 2015 00:54:30 -0400 Subject: Update README.md --- doc/api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc/api') diff --git a/doc/api/README.md b/doc/api/README.md index 131e50515..78dbc087d 100644 --- a/doc/api/README.md +++ b/doc/api/README.md @@ -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](./mruby.value.h.md)|`mrb_value` functions and macros. +[mruby/value.h](./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. -- cgit v1.2.3