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
44dc05f1
Commit
44dc05f1
authored
May 02, 2014
by
take_cheeze
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Struct#values_at and Array#values_at .
Add API `mrb_get_values_at()` to mruby/range.h .
parent
d91c9a9e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
0 deletions
+78
-0
include/mruby/range.h
include/mruby/range.h
+1
-0
mrbgems/mruby-array-ext/src/array.c
mrbgems/mruby-array-ext/src/array.c
+13
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+10
-0
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
+19
-0
mrbgems/mruby-struct/test/struct.rb
mrbgems/mruby-struct/test/struct.rb
+7
-0
src/range.c
src/range.c
+28
-0
No files found.
include/mruby/range.h
View file @
44dc05f1
...
...
@@ -27,6 +27,7 @@ struct RRange {
mrb_value
mrb_range_new
(
mrb_state
*
,
mrb_value
,
mrb_value
,
mrb_bool
);
mrb_bool
mrb_range_beg_len
(
mrb_state
*
mrb
,
mrb_value
range
,
mrb_int
*
begp
,
mrb_int
*
lenp
,
mrb_int
len
);
mrb_value
mrb_get_values_at
(
mrb_state
*
mrb
,
mrb_value
obj
,
mrb_int
olen
,
mrb_int
argc
,
const
mrb_value
*
argv
,
mrb_value
(
*
func
)(
mrb_state
*
,
mrb_value
,
mrb_int
));
#if defined(__cplusplus)
}
/* extern "C" { */
...
...
mrbgems/mruby-array-ext/src/array.c
View file @
44dc05f1
#include "mruby.h"
#include "mruby/value.h"
#include "mruby/array.h"
#include "mruby/range.h"
/*
* call-seq:
...
...
@@ -122,6 +123,17 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary)
return
mrb_ary_entry
(
ary
,
pos
);
}
static
mrb_value
mrb_ary_values_at
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_int
argc
;
mrb_value
*
argv
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
return
mrb_get_values_at
(
mrb
,
self
,
MRB_INT_MAX
,
argc
,
argv
,
mrb_ary_ref
);
}
void
mrb_mruby_array_ext_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -132,6 +144,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
a
,
"assoc"
,
mrb_ary_assoc
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"at"
,
mrb_ary_at
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"rassoc"
,
mrb_ary_rassoc
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"values_at"
,
mrb_ary_values_at
,
MRB_ARGS_ANY
());
}
void
...
...
mrbgems/mruby-array-ext/test/array.rb
View file @
44dc05f1
...
...
@@ -280,3 +280,13 @@ assert("Array#select!") do
assert_equal
[
4
,
5
],
a
.
select!
{
|
val
|
val
>
3
}
assert_equal
[
4
,
5
],
a
end
assert
(
'Array#values_at'
)
do
a
=
%w{red green purple white none}
assert_equal
%w{red purple none}
,
a
.
values_at
(
0
,
2
,
4
)
assert_equal
[
'green'
,
'white'
,
nil
,
nil
],
a
.
values_at
(
1
,
3
,
5
,
7
)
assert_equal
[
'none'
,
'white'
,
'white'
,
nil
],
a
.
values_at
(
-
1
,
-
2
,
-
2
,
-
7
)
assert_equal
[
'none'
,
nil
,
nil
,
'red'
,
'green'
,
'purple'
],
a
.
values_at
(
4
..
6
,
0
...
3
)
assert_raise
(
TypeError
)
{
a
.
values_at
'tt'
}
end
mrbgems/mruby-struct/src/struct.c
View file @
44dc05f1
...
...
@@ -12,6 +12,7 @@
#include "mruby/class.h"
#include "mruby/variable.h"
#include "mruby/hash.h"
#include "mruby/range.h"
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
...
...
@@ -828,6 +829,23 @@ mrb_struct_to_h(mrb_state *mrb, mrb_value self)
return
ret
;
}
static
mrb_value
struct_values_at_getter
(
mrb_state
*
mrb
,
mrb_value
self
,
mrb_int
idx
)
{
return
mrb_struct_aref_n
(
mrb
,
self
,
mrb_fixnum_value
(
idx
));
}
static
mrb_value
mrb_struct_values_at
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_int
argc
;
mrb_value
*
argv
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
return
mrb_get_values_at
(
mrb
,
self
,
MRB_INT_MAX
,
argc
,
argv
,
struct_values_at_getter
);
}
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
...
...
@@ -866,6 +884,7 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
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
());
mrb_define_method
(
mrb
,
st
,
"values_at"
,
mrb_struct_values_at
,
MRB_ARGS_NONE
());
}
void
...
...
mrbgems/mruby-struct/test/struct.rb
View file @
44dc05f1
...
...
@@ -123,3 +123,10 @@ 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
assert
(
'Struct#values_at'
)
do
a
=
Struct
.
new
(
:blue
,
:purple
).
new
(
'aki'
,
'io'
)
assert_equal
[
'aki'
],
a
.
values_at
(
0
)
assert_equal
[
'io'
,
'aki'
],
a
.
values_at
(
1
,
0
)
assert_raise
(
IndexError
)
{
a
.
values_at
2
}
end
src/range.c
View file @
44dc05f1
...
...
@@ -8,6 +8,7 @@
#include "mruby/class.h"
#include "mruby/range.h"
#include "mruby/string.h"
#include "mruby/array.h"
#define RANGE_CLASS (mrb_class_get(mrb, "Range"))
...
...
@@ -395,6 +396,33 @@ range_initialize_copy(mrb_state *mrb, mrb_value copy)
return
copy
;
}
mrb_value
mrb_get_values_at
(
mrb_state
*
mrb
,
mrb_value
obj
,
mrb_int
olen
,
mrb_int
argc
,
const
mrb_value
*
argv
,
mrb_value
(
*
func
)(
mrb_state
*
,
mrb_value
,
mrb_int
))
{
mrb_int
i
,
j
,
beg
,
len
;
mrb_value
result
;
result
=
mrb_ary_new
(
mrb
);
for
(
i
=
0
;
i
<
argc
;
++
i
)
{
if
(
mrb_fixnum_p
(
argv
[
i
]))
{
mrb_ary_push
(
mrb
,
result
,
func
(
mrb
,
obj
,
mrb_fixnum
(
argv
[
i
])));
}
else
if
(
mrb_range_beg_len
(
mrb
,
argv
[
i
],
&
beg
,
&
len
,
olen
))
{
mrb_int
const
end
=
RARRAY_LEN
(
obj
)
<
beg
+
len
?
RARRAY_LEN
(
obj
)
:
beg
+
len
;
for
(
j
=
beg
;
j
<
end
;
++
j
)
{
mrb_ary_push
(
mrb
,
result
,
func
(
mrb
,
obj
,
j
));
}
for
(;
j
<
beg
+
len
;
++
j
)
{
mrb_ary_push
(
mrb
,
result
,
mrb_nil_value
());
}
}
else
{
mrb_raisef
(
mrb
,
E_ARGUMENT_ERROR
,
"invalid values selector: %S"
,
argv
[
i
]);
}
}
return
result
;
}
void
mrb_init_range
(
mrb_state
*
mrb
)
{
...
...
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