@@ -1428,20 +1428,11 @@ enum ary_take_pos_flags
1428
1428
};
1429
1429
1430
1430
static VALUE
1431
- ary_take_first_or_last ( int argc , const VALUE * argv , VALUE ary , enum ary_take_pos_flags last )
1431
+ ary_take_first_or_last_n ( VALUE ary , long n , enum ary_take_pos_flags last )
1432
1432
{
1433
- long n ;
1434
- long len ;
1433
+ long len = RARRAY_LEN (ary );
1435
1434
long offset = 0 ;
1436
1435
1437
- argc = rb_check_arity (argc , 0 , 1 );
1438
- /* the case optional argument is omitted should be handled in
1439
- * callers of this function. if another arity case is added,
1440
- * this arity check needs to rewrite. */
1441
- RUBY_ASSERT_ALWAYS (argc == 1 );
1442
-
1443
- n = NUM2LONG (argv [0 ]);
1444
- len = RARRAY_LEN (ary );
1445
1436
if (n > len ) {
1446
1437
n = len ;
1447
1438
}
@@ -1454,6 +1445,17 @@ ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos
1454
1445
return ary_make_partial (ary , rb_cArray , offset , n );
1455
1446
}
1456
1447
1448
+ static VALUE
1449
+ ary_take_first_or_last (int argc , const VALUE * argv , VALUE ary , enum ary_take_pos_flags last )
1450
+ {
1451
+ argc = rb_check_arity (argc , 0 , 1 );
1452
+ /* the case optional argument is omitted should be handled in
1453
+ * callers of this function. if another arity case is added,
1454
+ * this arity check needs to rewrite. */
1455
+ RUBY_ASSERT_ALWAYS (argc == 1 );
1456
+ return ary_take_first_or_last_n (ary , NUM2LONG (argv [0 ]), last );
1457
+ }
1458
+
1457
1459
/*
1458
1460
* call-seq:
1459
1461
* array << object -> self
@@ -2027,6 +2029,7 @@ rb_ary_at(VALUE ary, VALUE pos)
2027
2029
return rb_ary_entry (ary , NUM2LONG (pos ));
2028
2030
}
2029
2031
2032
+ #if 0
2030
2033
/*
2031
2034
* call-seq:
2032
2035
* array.first -> object or nil
@@ -2071,6 +2074,20 @@ rb_ary_first(int argc, VALUE *argv, VALUE ary)
2071
2074
return ary_take_first_or_last (argc , argv , ary , ARY_TAKE_FIRST );
2072
2075
}
2073
2076
}
2077
+ #endif
2078
+
2079
+ static VALUE
2080
+ ary_first (VALUE self )
2081
+ {
2082
+ return (RARRAY_LEN (self ) == 0 ) ? Qnil : RARRAY_AREF (self , 0 );
2083
+ }
2084
+
2085
+ static VALUE
2086
+ ary_last (VALUE self )
2087
+ {
2088
+ long len = RARRAY_LEN (self );
2089
+ return (len == 0 ) ? Qnil : RARRAY_AREF (self , len - 1 );
2090
+ }
2074
2091
2075
2092
/*
2076
2093
* call-seq:
@@ -2107,12 +2124,10 @@ rb_ary_first(int argc, VALUE *argv, VALUE ary)
2107
2124
*/
2108
2125
2109
2126
VALUE
2110
- rb_ary_last (int argc , const VALUE * argv , VALUE ary )
2127
+ rb_ary_last (int argc , const VALUE * argv , VALUE ary ) // used by parse.y
2111
2128
{
2112
2129
if (argc == 0 ) {
2113
- long len = RARRAY_LEN (ary );
2114
- if (len == 0 ) return Qnil ;
2115
- return RARRAY_AREF (ary , len - 1 );
2130
+ return ary_last (ary );
2116
2131
}
2117
2132
else {
2118
2133
return ary_take_first_or_last (argc , argv , ary , ARY_TAKE_LAST );
@@ -8809,8 +8824,6 @@ Init_Array(void)
8809
8824
rb_define_method (rb_cArray , "[]=" , rb_ary_aset , -1 );
8810
8825
rb_define_method (rb_cArray , "at" , rb_ary_at , 1 );
8811
8826
rb_define_method (rb_cArray , "fetch" , rb_ary_fetch , -1 );
8812
- rb_define_method (rb_cArray , "first" , rb_ary_first , -1 );
8813
- rb_define_method (rb_cArray , "last" , rb_ary_last , -1 );
8814
8827
rb_define_method (rb_cArray , "concat" , rb_ary_concat_multi , -1 );
8815
8828
rb_define_method (rb_cArray , "union" , rb_ary_union_multi , -1 );
8816
8829
rb_define_method (rb_cArray , "difference" , rb_ary_difference_multi , -1 );
0 commit comments