diff options
| author | take_cheeze <[email protected]> | 2014-05-02 23:20:48 +0900 |
|---|---|---|
| committer | take_cheeze <[email protected]> | 2014-05-02 23:20:48 +0900 |
| commit | 44dc05f12a06e329119b6bf5606e4836b653c48f (patch) | |
| tree | 1dcf10c618c387c61d956e75e5e017495b7ce6d2 /mrbgems/mruby-struct/src/struct.c | |
| parent | d91c9a9ea41349b2455ed89e4fbd46de0374c53b (diff) | |
| download | mruby-44dc05f12a06e329119b6bf5606e4836b653c48f.tar.gz mruby-44dc05f12a06e329119b6bf5606e4836b653c48f.zip | |
Implement Struct#values_at and Array#values_at .
Add API `mrb_get_values_at()` to mruby/range.h .
Diffstat (limited to 'mrbgems/mruby-struct/src/struct.c')
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index a4d70ae1a..40ad88aca 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -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 |
