summaryrefslogtreecommitdiffhomepage
path: root/doc
diff options
context:
space:
mode:
authorRalph Desir <[email protected]>2015-07-08 05:35:37 -0400
committerRalph Desir <[email protected]>2015-07-08 05:35:37 -0400
commit82380780db07e9d776d3d7983a229a114f30ecb8 (patch)
tree5d85663d8499c7cc607c0154b3dc841b9fc6b7cb /doc
parent2523ed473af8d9e97c641f7263286d1af40a69a3 (diff)
downloadmruby-82380780db07e9d776d3d7983a229a114f30ecb8.tar.gz
mruby-82380780db07e9d776d3d7983a229a114f30ecb8.zip
Update hash.h.md
Diffstat (limited to 'doc')
-rw-r--r--doc/api/mruby/hash.h.md96
1 files changed, 96 insertions, 0 deletions
diff --git a/doc/api/mruby/hash.h.md b/doc/api/mruby/hash.h.md
index e69de29bb..1d713d59e 100644
--- a/doc/api/mruby/hash.h.md
+++ b/doc/api/mruby/hash.h.md
@@ -0,0 +1,96 @@
+### 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
+#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
+```
+
+