mrb_value mrb_hash_new(mrb_state *mrb);
Initializes a hash.
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.
#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; }
class Example_Class def method_name(a) puts a puts a.class end end Example_Class.new
void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
Sets a keys and values to hashes.
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:
a = {:da_key => 80}
#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; }
class Example_Class def method_name(a) puts a puts a.class end end Example_Class.new
After compiling you should get these results.
{:da_key=>80} Hash
mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
Gets a value from a key.
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:
a = {:da_key => 80} a[:da_key]
#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; }
class Example_Class def method_name(a) puts a puts a.class end end Example_Class.new
After compiling you should get these results.
90 Fixnum
mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
Deletes hash key and value pair.
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:
a = {:da_key1 => 80,:da_key2 => 90} a.delete(:da_key2)
#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; }
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
After compiling you should get these results.
Hash pre deletion {:da_key1 => 80, :da_key2 => 90} Hash post deletion {:da_key1 => 80}
mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash);
Gets an array of keys.
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.
#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; }
class Example_Class def method_name(a) puts a puts a.class end end Example_Class.new
After compiling you should get these results.
[:da_key1, :da_key2] Array
mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
Clears the hash.
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:
a = {:da_key1 => 80,:da_key2 => 90} a.clear
#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; }
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
After compiling you should get these results.
Hash pre clear {:da_key1 => 80, :da_key2 => 90} Hash post clear {}
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.