Skip to content

Commit

Permalink
* range.c (range_first): takes first n element if argument is
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
matz committed Oct 18, 2007
1 parent ae7ea35 commit fada885
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
4 changes: 2 additions & 2 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions include/ruby/intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
72 changes: 65 additions & 7 deletions range.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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>.
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions version.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#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
#define RUBY_VERSION_MINOR 9
#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[];
Expand Down

0 comments on commit fada885

Please sign in to comment.