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
e2f6a905
Commit
e2f6a905
authored
Apr 27, 2014
by
take_cheeze
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Struct#to_a and Struct#values .
parent
bbc23704
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
0 deletions
+21
-0
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
+15
-0
mrbgems/mruby-struct/test/struct.rb
mrbgems/mruby-struct/test/struct.rb
+6
-0
No files found.
mrbgems/mruby-struct/src/struct.c
View file @
e2f6a905
...
@@ -792,6 +792,19 @@ mrb_struct_len(mrb_state *mrb, mrb_value self)
...
@@ -792,6 +792,19 @@ mrb_struct_len(mrb_state *mrb, mrb_value self)
return
mrb_fixnum_value
(
RSTRUCT_LEN
(
self
));
return
mrb_fixnum_value
(
RSTRUCT_LEN
(
self
));
}
}
/*
* call-seq:
* struct.to_a -> array
* struct.values -> array
*
* Create an array from struct values.
*/
static
mrb_value
mrb_struct_to_a
(
mrb_state
*
mrb
,
mrb_value
self
)
{
return
mrb_ary_new_from_values
(
mrb
,
RSTRUCT_LEN
(
self
),
RSTRUCT_PTR
(
self
));
}
/*
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
* attributes together, using accessor methods, without having to write
...
@@ -827,6 +840,8 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
...
@@ -827,6 +840,8 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
st
,
"size"
,
mrb_struct_len
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
st
,
"size"
,
mrb_struct_len
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
st
,
"length"
,
mrb_struct_len
,
MRB_ARGS_NONE
());
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
());
}
}
void
void
...
...
mrbgems/mruby-struct/test/struct.rb
View file @
e2f6a905
...
@@ -112,3 +112,9 @@ assert('Struct#length, Struct#size') do
...
@@ -112,3 +112,9 @@ assert('Struct#length, Struct#size') do
assert_equal
2
,
s
.
size
assert_equal
2
,
s
.
size
assert_equal
2
,
s
.
length
assert_equal
2
,
s
.
length
end
end
assert
(
'Struct#to_a, Struct#values'
)
do
s
=
Struct
.
new
(
:mem1
,
:mem2
).
new
(
'a'
,
'b'
)
assert_equal
[
'a'
,
'b'
],
s
.
to_a
assert_equal
[
'a'
,
'b'
],
s
.
values
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