Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
82380780
Commit
82380780
authored
Jul 08, 2015
by
Ralph Desir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update hash.h.md
parent
2523ed47
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
doc/api/mruby/hash.h.md
doc/api/mruby/hash.h.md
+96
-0
No files found.
doc/api/mruby/hash.h.md
View file @
82380780
### 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
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment