1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* range.c (range_first): takes first n element if argument is

given.  [ruby-core:12697]

* range.c (range_last): returns last n elements if argument is
  given.

* array.c (rb_ary_subseq, rb_ary_last): export.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-10-18 06:58:35 +00:00
parent ae7ea35916
commit fada885be6
6 changed files with 82 additions and 13 deletions

View file

@ -1,3 +1,13 @@
Thu Oct 18 09:33:25 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* range.c (range_first): takes first n element if argument is
given. [ruby-core:12697]
* range.c (range_last): returns last n elements if argument is
given.
* array.c (rb_ary_subseq, rb_ary_last): export.
Wed Oct 17 17:39:31 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ruby.c (proc_options): fixed reversed condition. [ruby-core:12722]

View file

@ -668,7 +668,7 @@ rb_ary_entry(VALUE ary, long offset)
return rb_ary_elt(ary, offset);
}
static VALUE
VALUE
rb_ary_subseq(VALUE ary, long beg, long len)
{
VALUE klass, ary2, shared;
@ -820,7 +820,7 @@ rb_ary_first(int argc, VALUE *argv, VALUE ary)
* a.last(2) #=> ["y", "z"]
*/
static VALUE
VALUE
rb_ary_last(int argc, VALUE *argv, VALUE ary)
{
if (argc == 0) {

2
enum.c
View file

@ -555,7 +555,7 @@ enum_first(int argc, VALUE *argv, VALUE obj)
rb_scan_args(argc, argv, "01", &n);
if (NIL_P(n)) {
if (argc == 0) {
ary[0] = ary[1] = Qnil;
}
else {

View file

@ -47,6 +47,7 @@ VALUE rb_ary_new4(long, const VALUE *);
void rb_ary_free(VALUE);
VALUE rb_ary_freeze(VALUE);
VALUE rb_ary_aref(int, VALUE*, VALUE);
VALUE rb_ary_subseq(VALUE, long, long);
void rb_ary_store(VALUE, long, VALUE);
VALUE rb_ary_dup(VALUE);
VALUE rb_ary_to_ary(VALUE);

72
range.c
View file

@ -437,14 +437,13 @@ range_each(VALUE range)
/*
* call-seq:
* rng.first => obj
* rng.begin => obj
*
* Returns the first object in <i>rng</i>.
*/
static VALUE
range_first(VALUE range)
range_begin(VALUE range)
{
return RANGE_BEG(range);
}
@ -453,7 +452,6 @@ range_first(VALUE range)
/*
* call-seq:
* rng.end => obj
* rng.last => obj
*
* Returns the object that defines the end of <i>rng</i>.
*
@ -463,11 +461,71 @@ range_first(VALUE range)
static VALUE
range_last(VALUE range)
range_end(VALUE range)
{
return RANGE_END(range);
}
static VALUE
first_i(VALUE i, VALUE *ary)
{
long n = NUM2LONG(ary[0]);
if (n <= 0) {
rb_iter_break();
}
rb_ary_push(ary[1], i);
n--;
ary[0] = INT2NUM(n);
return Qnil;
}
/*
* call-seq:
* rng.first => obj
* rng.first(n) => an_array
*
* Returns the first object in <i>rng</i>, or the first +n+ elements.
*/
static VALUE
range_first(int argc, VALUE *argv, VALUE range)
{
VALUE n, ary[2];
if (argc == 0) return RANGE_BEG(range);
rb_scan_args(argc, argv, "1", &n);
ary[0] = n;
ary[1] = rb_ary_new2(NUM2LONG(n));
rb_block_call(range, rb_intern("each"), 0, 0, first_i, (VALUE)ary);
return ary[1];
}
/*
* call-seq:
* rng.last => obj
* rng.last(n) => an_array
*
* Returns the last object in <i>rng</i>, or the last +n+ elements.
*/
static VALUE
range_last(int argc, VALUE *argv, VALUE range)
{
VALUE n, a;
long i, nelem, len;
if (argc == 0) return RANGE_END(range);
rb_scan_args(argc, argv, "1", &n);
return rb_ary_last(argc, argv, rb_Array(range));
}
/*
* call-seq:
* rng.min => obj
@ -858,10 +916,10 @@ Init_Range(void)
rb_define_method(rb_cRange, "hash", range_hash, 0);
rb_define_method(rb_cRange, "each", range_each, 0);
rb_define_method(rb_cRange, "step", range_step, -1);
rb_define_method(rb_cRange, "first", range_first, 0);
rb_define_method(rb_cRange, "last", range_last, 0);
rb_define_method(rb_cRange, "begin", range_first, 0);
rb_define_method(rb_cRange, "begin", range_begin, 0);
rb_define_method(rb_cRange, "end", range_last, 0);
rb_define_method(rb_cRange, "first", range_first, -1);
rb_define_method(rb_cRange, "last", range_last, -1);
rb_define_method(rb_cRange, "min", range_min, 0);
rb_define_method(rb_cRange, "max", range_max, 0);
rb_define_method(rb_cRange, "to_s", range_to_s, 0);

View file

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2007-10-17"
#define RUBY_RELEASE_DATE "2007-10-18"
#define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20071017
#define RUBY_RELEASE_CODE 20071018
#define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 10
#define RUBY_RELEASE_DAY 17
#define RUBY_RELEASE_DAY 18
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];