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
90d30f30
Commit
90d30f30
authored
Apr 30, 2014
by
take_cheeze
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Struct#to_h .
parent
2cb54e4b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
+24
-0
mrbgems/mruby-struct/test/struct.rb
mrbgems/mruby-struct/test/struct.rb
+5
-0
No files found.
mrbgems/mruby-struct/src/struct.c
View file @
90d30f30
...
...
@@ -11,6 +11,7 @@
#include "mruby/string.h"
#include "mruby/class.h"
#include "mruby/variable.h"
#include "mruby/hash.h"
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
...
...
@@ -805,6 +806,28 @@ mrb_struct_to_a(mrb_state *mrb, mrb_value self)
return
mrb_ary_new_from_values
(
mrb
,
RSTRUCT_LEN
(
self
),
RSTRUCT_PTR
(
self
));
}
/*
* call-seq:
* struct.to_h -> hash
*
* Create a hash from member names and struct values.
*/
static
mrb_value
mrb_struct_to_h
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
members
,
ret
;
mrb_int
i
;
members
=
mrb_struct_s_members
(
mrb
,
mrb_obj_value
(
mrb_class
(
mrb
,
self
)));
ret
=
mrb_hash_new_capa
(
mrb
,
RARRAY_LEN
(
members
));
for
(
i
=
0
;
i
<
RARRAY_LEN
(
members
);
++
i
)
{
mrb_hash_set
(
mrb
,
ret
,
RARRAY_PTR
(
members
)[
i
],
RSTRUCT_PTR
(
self
)[
i
]);
}
return
ret
;
}
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
...
...
@@ -842,6 +865,7 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
st
,
"length"
,
mrb_struct_len
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
st
,
"to_a"
,
mrb_struct_to_a
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
st
,
"values"
,
mrb_struct_to_a
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
st
,
"to_h"
,
mrb_struct_to_h
,
MRB_ARGS_NONE
());
}
void
...
...
mrbgems/mruby-struct/test/struct.rb
View file @
90d30f30
...
...
@@ -118,3 +118,8 @@ assert('Struct#to_a, Struct#values') do
assert_equal
[
'a'
,
'b'
],
s
.
to_a
assert_equal
[
'a'
,
'b'
],
s
.
values
end
assert
(
'Struct#to_h'
)
do
s
=
Struct
.
new
(
:white
,
:red
,
:green
).
new
(
'ruuko'
,
'yuzuki'
,
'hitoe'
)
assert_equal
(
:white
=>
'ruuko'
,
:red
=>
'yuzuki'
,
:green
=>
'hitoe'
)
{
s
.
to_h
}
end
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