summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-06 09:24:59 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-06 09:24:59 +0000
commit9e77e91a13a8ae1d8bb4bba8974da390fbadedc0 (patch)
tree53dafc7d4f53ccc940badbc36862ad24cc1da929 /array.c
parent1fa5c630cda65f69d2ccffb614f45b5e9470b167 (diff)
* eval.c (ruby_cleanup): $SAFE is turned off in the finalization.
Each END proc should preserve its own $SAFE level. [ruby-core:01119] * marshal.c (marshal_load): remove unused variable "hash". [ruby-core:01120] * hash.c (env_str_new): freeze strings from ENV. [ruby-talk:72860] * array.c (rb_ary_first): optional argument to retrieve first n elements. * array.c (rb_ary_last): optional argument to retrieve last n elements. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c50
1 files changed, 42 insertions, 8 deletions
diff --git a/array.c b/array.c
index e0b5af8..5aa268b 100644
--- a/array.c
+++ b/array.c
@@ -534,19 +534,53 @@ rb_ary_at(ary, pos)
}
static VALUE
-rb_ary_first(ary)
+rb_ary_first(argc, argv, ary)
+ int argc;
+ VALUE *argv;
VALUE ary;
{
- if (RARRAY(ary)->len == 0) return Qnil;
- return RARRAY(ary)->ptr[0];
+ if (argc == 0) {
+ if (RARRAY(ary)->len == 0) return Qnil;
+ return RARRAY(ary)->ptr[0];
+ }
+ else {
+ VALUE nv, result;
+ long n, i;
+
+ rb_scan_args(argc, argv, "01", &nv);
+ n = NUM2LONG(nv);
+ if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
+ result = rb_ary_new2(n);
+ for (i=0; i<n; i++) {
+ rb_ary_push(result, RARRAY(ary)->ptr[i]);
+ }
+ return result;
+ }
}
static VALUE
-rb_ary_last(ary)
+rb_ary_last(argc, argv, ary)
+ int argc;
+ VALUE *argv;
VALUE ary;
{
- if (RARRAY(ary)->len == 0) return Qnil;
- return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
+ if (argc == 0) {
+ if (RARRAY(ary)->len == 0) return Qnil;
+ return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
+ }
+ else {
+ VALUE nv, result;
+ long n, i;
+
+ rb_scan_args(argc, argv, "01", &nv);
+ n = NUM2LONG(nv);
+ if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
+ result = rb_ary_new2(n);
+ for (i=RARRAY(ary)->len-n; n--; i++) {
+ rb_ary_push(result, RARRAY(ary)->ptr[i]);
+ }
+ return result;
+ }
}
static VALUE
@@ -1984,8 +2018,8 @@ Init_Array()
rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1);
rb_define_method(rb_cArray, "at", rb_ary_at, 1);
rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1);
- rb_define_method(rb_cArray, "first", rb_ary_first, 0);
- rb_define_method(rb_cArray, "last", rb_ary_last, 0);
+ rb_define_method(rb_cArray, "first", rb_ary_first, -1);
+ rb_define_method(rb_cArray, "last", rb_ary_last, -1);
rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);