@@ -3952,53 +3952,24 @@ rb_arithmetic_sequence_beg_len_step(VALUE obj, long *begp, long *lenp, long *ste
3952
3952
return Qnil ;
3953
3953
}
3954
3954
3955
- /*
3956
- * call-seq:
3957
- * aseq.first -> num or nil
3958
- * aseq.first(n) -> an_array
3959
- *
3960
- * Returns the first number in this arithmetic sequence,
3961
- * or an array of the first +n+ elements.
3962
- */
3963
3955
static VALUE
3964
- arith_seq_first ( int argc , VALUE * argv , VALUE self )
3956
+ arith_seq_take ( VALUE self , VALUE num )
3965
3957
{
3966
3958
VALUE b , e , s , ary ;
3967
3959
long n ;
3968
3960
int x ;
3969
3961
3970
- rb_check_arity (argc , 0 , 1 );
3971
-
3972
- b = arith_seq_begin (self );
3973
- e = arith_seq_end (self );
3974
- s = arith_seq_step (self );
3975
- if (argc == 0 ) {
3976
- if (NIL_P (b )) {
3977
- return Qnil ;
3978
- }
3979
- if (!NIL_P (e )) {
3980
- VALUE zero = INT2FIX (0 );
3981
- int r = rb_cmpint (rb_num_coerce_cmp (s , zero , idCmp ), s , zero );
3982
- if (r > 0 && RTEST (rb_funcall (b , '>' , 1 , e ))) {
3983
- return Qnil ;
3984
- }
3985
- if (r < 0 && RTEST (rb_funcall (b , '<' , 1 , e ))) {
3986
- return Qnil ;
3987
- }
3988
- }
3989
- return b ;
3990
- }
3991
-
3992
- // TODO: the following code should be extracted as arith_seq_take
3993
-
3994
- n = NUM2LONG (argv [0 ]);
3962
+ n = NUM2LONG (num );
3995
3963
if (n < 0 ) {
3996
3964
rb_raise (rb_eArgError , "attempt to take negative size" );
3997
3965
}
3998
3966
if (n == 0 ) {
3999
3967
return rb_ary_new_capa (0 );
4000
3968
}
4001
3969
3970
+ b = arith_seq_begin (self );
3971
+ e = arith_seq_end (self );
3972
+ s = arith_seq_step (self );
4002
3973
x = arith_seq_exclude_end_p (self );
4003
3974
4004
3975
if (FIXNUM_P (b ) && NIL_P (e ) && FIXNUM_P (s )) {
@@ -4093,7 +4064,49 @@ arith_seq_first(int argc, VALUE *argv, VALUE self)
4093
4064
return ary ;
4094
4065
}
4095
4066
4096
- return rb_call_super (argc , argv );
4067
+ {
4068
+ VALUE argv [1 ];
4069
+ argv [0 ] = num ;
4070
+ return rb_call_super (1 , argv );
4071
+ }
4072
+ }
4073
+
4074
+ /*
4075
+ * call-seq:
4076
+ * aseq.first -> num or nil
4077
+ * aseq.first(n) -> an_array
4078
+ *
4079
+ * Returns the first number in this arithmetic sequence,
4080
+ * or an array of the first +n+ elements.
4081
+ */
4082
+ static VALUE
4083
+ arith_seq_first (int argc , VALUE * argv , VALUE self )
4084
+ {
4085
+ VALUE b , e , s ;
4086
+
4087
+ rb_check_arity (argc , 0 , 1 );
4088
+
4089
+ b = arith_seq_begin (self );
4090
+ e = arith_seq_end (self );
4091
+ s = arith_seq_step (self );
4092
+ if (argc == 0 ) {
4093
+ if (NIL_P (b )) {
4094
+ return Qnil ;
4095
+ }
4096
+ if (!NIL_P (e )) {
4097
+ VALUE zero = INT2FIX (0 );
4098
+ int r = rb_cmpint (rb_num_coerce_cmp (s , zero , idCmp ), s , zero );
4099
+ if (r > 0 && RTEST (rb_funcall (b , '>' , 1 , e ))) {
4100
+ return Qnil ;
4101
+ }
4102
+ if (r < 0 && RTEST (rb_funcall (b , '<' , 1 , e ))) {
4103
+ return Qnil ;
4104
+ }
4105
+ }
4106
+ return b ;
4107
+ }
4108
+
4109
+ return arith_seq_take (self , argv [0 ]);
4097
4110
}
4098
4111
4099
4112
static inline VALUE
0 commit comments