summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c39
1 files changed, 26 insertions, 13 deletions
diff --git a/struct.c b/struct.c
index b1fa31fbd2..d3467bc67b 100644
--- a/struct.c
+++ b/struct.c
@@ -529,6 +529,19 @@ rb_struct_aset(s, idx, val)
return RSTRUCT(s)->ptr[i] = val;
}
+static VALUE
+rb_struct_values_at(argc, argv, s)
+ int argc;
+ VALUE *argv;
+ VALUE s;
+{
+ VALUE result = rb_ary_new();
+ long i;
+
+ for (i=0; i<argc; i++) {
+ rb_ary_push(result, rb_struct_aref(s, argv[i]));
+ }
+}
static VALUE
rb_struct_select(argc, argv, s)
@@ -536,24 +549,23 @@ rb_struct_select(argc, argv, s)
VALUE *argv;
VALUE s;
{
- VALUE result = rb_ary_new();
+ VALUE result;
long i;
- if (rb_block_given_p()) {
- if (argc > 0) {
- rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
- }
- for (i = 0; i < RSTRUCT(s)->len; i++) {
- if (RTEST(rb_yield(RSTRUCT(s)->ptr[i]))) {
- rb_ary_push(result, RSTRUCT(s)->ptr[i]);
- }
- }
+ if (!rb_block_given_p()) {
+ rb_warn("Struct#select(index..) is deprecated; use Struct#values_at");
+ return rb_struct_values_at(argc, argv, s);
}
- else {
- for (i=0; i<argc; i++) {
- rb_ary_push(result, rb_struct_aref(s, argv[i]));
+ if (argc > 0) {
+ rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
+ }
+ result = rb_ary_new();
+ for (i = 0; i < RSTRUCT(s)->len; i++) {
+ if (RTEST(rb_yield(RSTRUCT(s)->ptr[i]))) {
+ rb_ary_push(result, RSTRUCT(s)->ptr[i]);
}
}
+
return result;
}
@@ -646,6 +658,7 @@ Init_Struct()
rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
rb_define_method(rb_cStruct, "select", rb_struct_select, -1);
+ rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
rb_define_method(rb_cStruct, "members", rb_struct_members, 0);
}