ruby-changes:2245
From: ko1@a...
Date: 18 Oct 2007 15:58:53 +0900
Subject: [ruby-changes:2245] matz - Ruby:r13736 (trunk): * range.c (range_first): takes first n element if argument is
matz 2007-10-18 15:58:35 +0900 (Thu, 18 Oct 2007)
New Revision: 13736
Modified files:
trunk/ChangeLog
trunk/array.c
trunk/enum.c
trunk/include/ruby/intern.h
trunk/range.c
trunk/version.h
Log:
* 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.
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/version.h?r1=13736&r2=13735
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/array.c?r1=13736&r2=13735
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13736&r2=13735
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/range.c?r1=13736&r2=13735
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enum.c?r1=13736&r2=13735
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/intern.h?r1=13736&r2=13735
Index: array.c
===================================================================
--- array.c (revision 13735)
+++ array.c (revision 13736)
@@ -668,7 +668,7 @@
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 @@
* a.last(2) #=> ["y", "z"]
*/
-static VALUE
+VALUE
rb_ary_last(int argc, VALUE *argv, VALUE ary)
{
if (argc == 0) {
Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h (revision 13735)
+++ include/ruby/intern.h (revision 13736)
@@ -47,6 +47,7 @@
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);
Index: ChangeLog
===================================================================
--- ChangeLog (revision 13735)
+++ ChangeLog (revision 13736)
@@ -1,3 +1,13 @@
+Thu Oct 18 09:33:25 2007 Yukihiro Matsumoto <matz@r...>
+
+ * 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@r...>
* ruby.c (proc_options): fixed reversed condition. [ruby-core:12722]
Index: enum.c
===================================================================
--- enum.c (revision 13735)
+++ enum.c (revision 13736)
@@ -555,7 +555,7 @@
rb_scan_args(argc, argv, "01", &n);
- if (NIL_P(n)) {
+ if (argc == 0) {
ary[0] = ary[1] = Qnil;
}
else {
Index: range.c
===================================================================
--- range.c (revision 13735)
+++ range.c (revision 13736)
@@ -437,14 +437,13 @@
/*
* 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 @@
/*
* call-seq:
* rng.end => obj
- * rng.last => obj
*
* Returns the object that defines the end of <i>rng</i>.
*
@@ -463,13 +461,73 @@
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
* rng.min {| a,b | block } => obj
*
@@ -858,10 +916,10 @@
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);
Index: version.h
===================================================================
--- version.h (revision 13735)
+++ version.h (revision 13736)
@@ -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[];
--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml