From 40efee52b5b7af7c40fa26098157469c4f066153 Mon Sep 17 00:00:00 2001 From: matz Date: Sun, 4 May 2003 16:03:24 +0000 Subject: * array.c (rb_ary_values_at): new method to replace select(index..). * hash.c (rb_hash_values_at,env_values_at): ditto. * re.c (match_values_at): ditto. * struct.c (rb_struct_values_at): ditto. * re.c (match_select): add iterator behavior. * ext/curses/curses.c, ext/digest/sha2/sha2.c, ext/iconv/iconv.c, ext/racc/cparse/cparse.c: include "ruby.h" at the top to shut up "_FILE_OFFSET_BITS redefined" warning on Solaris. * class.c (rb_class_protected_instance_methods): now gives warnings to show migration path. The default will be reversed on Jan 2004. * numeric.c (num_step): "1.1.step(1.5,0.1)" to work. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@3754 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- struct.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index b1fa31fbd..d3467bc67 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 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 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); } -- cgit