[macruby-changes] [4049] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri May 7 19:20:39 PDT 2010


Revision: 4049
          http://trac.macosforge.org/projects/ruby/changeset/4049
Author:   martinlagardette at apple.com
Date:     2010-05-07 19:20:37 -0700 (Fri, 07 May 2010)
Log Message:
-----------
Improve RubySpec coverage: Backport various Range fixes

 - Treat symbols so that they work like strings
 - Fixed equality to work for subclasses of Range
 - max value for exclusive ranges should not be defined for non Integer / Numeric end values

Modified Paths:
--------------
    MacRuby/trunk/enum.c
    MacRuby/trunk/range.c

Removed Paths:
-------------
    MacRuby/trunk/spec/frozen/tags/macruby/core/range/eql_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/range/equal_value_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/range/to_a_tags.txt

Modified: MacRuby/trunk/enum.c
===================================================================
--- MacRuby/trunk/enum.c	2010-05-08 01:30:20 UTC (rev 4048)
+++ MacRuby/trunk/enum.c	2010-05-08 02:20:37 UTC (rev 4049)
@@ -1053,6 +1053,7 @@
     VALUE cmp;
 
     ENUM_WANT_SVALUE();
+    printf("max_i\n");
 
     if (*memo == Qundef) {
 	*memo = i;

Modified: MacRuby/trunk/range.c
===================================================================
--- MacRuby/trunk/range.c	2010-05-08 01:30:20 UTC (rev 4048)
+++ MacRuby/trunk/range.c	2010-05-08 02:20:37 UTC (rev 4049)
@@ -135,18 +135,23 @@
 static VALUE
 range_eq(VALUE range, SEL sel, VALUE obj)
 {
-    if (range == obj)
+    if (range == obj) {
 	return Qtrue;
-    if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
+    }
+    if (!rb_obj_is_kind_of(obj, rb_cRange)) {
 	return Qfalse;
+    }
 
-    if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
+    if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj))) {
 	return Qfalse;
-    if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
+    }
+    if (!rb_equal(RANGE_END(range), RANGE_END(obj))) {
 	return Qfalse;
+    }
 
-    if (EXCL(range) != EXCL(obj))
+    if (EXCL(range) != EXCL(obj)) {
 	return Qfalse;
+    }
 
     return Qtrue;
 }
@@ -197,18 +202,23 @@
 static VALUE
 range_eql(VALUE range, SEL sel, VALUE obj)
 {
-    if (range == obj)
+    if (range == obj) {
 	return Qtrue;
-    if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
+    }
+    if (!rb_obj_is_kind_of(obj, rb_cRange)) {
 	return Qfalse;
+    }
 
-    if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
+    if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj))) {
 	return Qfalse;
-    if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
+    }
+    if (!rb_eql(RANGE_END(range), RANGE_END(obj))) {
 	return Qfalse;
+    }
 
-    if (EXCL(range) != EXCL(obj))
+    if (EXCL(range) != EXCL(obj)) {
 	return Qfalse;
+    }
 
     return Qtrue;
 }
@@ -280,6 +290,25 @@
     return Qnil;
 }
 
+static VALUE
+sym_step_i(VALUE i, void *arg)
+{
+    VALUE *iter = arg;
+
+    if (FIXNUM_P(iter[0])) {
+	iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
+    }
+    else {
+	VALUE one = INT2FIX(1);
+	iter[0] = rb_vm_call(iter[0], selMINUS,1, &one, false);
+    }
+    if (iter[0] == INT2FIX(0)) {
+	rb_yield(ID2SYM(rb_intern_str(i)));
+	iter[0] = iter[1];
+    }
+    return Qnil;
+}
+
 /*
  *  call-seq:
  *     rng.step(n=1) {| obj | block }    => rng
@@ -349,6 +378,16 @@
 	}
 
     }
+    else if (SYMBOL_P(b) && SYMBOL_P(e)) { /* symbols are special */
+	VALUE args[2];
+	VALUE iter[2];
+
+	args[0] = rb_sym_to_s(e);
+	args[1] = EXCL(range) ? Qtrue : Qfalse;
+	iter[0] = INT2FIX(1);
+	iter[1] = step;
+	rb_objc_block_call(rb_sym_to_s(b), selUpto, cacheUpto, 2, args, sym_step_i, (VALUE)iter);
+    }
     else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
 	     !NIL_P(rb_check_to_integer(b, "to_int")) ||
 	     !NIL_P(rb_check_to_integer(e, "to_int"))) {
@@ -395,6 +434,13 @@
     return Qnil;
 }
 
+static VALUE
+sym_each_i(VALUE v, void *arg)
+{
+    rb_yield(ID2SYM(rb_intern_str(v)));
+    return Qnil;
+}
+
 /*
  *  call-seq:
  *     rng.each {| i | block } => rng
@@ -438,6 +484,13 @@
 	    RETURN_IF_BROKEN();
 	}
     }
+    else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
+	VALUE args[2];
+
+	args[0] = rb_sym_to_s(end);
+	args[1] = EXCL(range) ? Qtrue : Qfalse;
+	rb_objc_block_call(rb_sym_to_s(beg), selUpto, cacheUpto, 2, args, sym_each_i, 0);
+    }
     else if (TYPE(beg) == T_STRING) {
 	VALUE args[2];
 
@@ -588,9 +641,9 @@
 range_max(VALUE range, SEL sel)
 {
     VALUE e = RANGE_END(range);
-    int ip = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cInteger);
+    int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
 
-    if (rb_block_given_p() || (EXCL(range) && !ip)) {
+    if (rb_block_given_p() || (EXCL(range) && !nm)) {
 	//return rb_call_super(0, 0);
 	if (sel == NULL) {
 	    sel = sel_registerName("max");
@@ -604,7 +657,12 @@
 	if (c > 0)
 	    return Qnil;
 	if (EXCL(range)) {
-	    if (c == 0) return Qnil;
+	    if (!FIXNUM_P(e) && !rb_obj_is_kind_of(e, rb_cInteger)) {
+		rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
+	    }
+	    if (c == 0) {
+		return Qnil;
+	    }
 	    if (FIXNUM_P(e)) {
 		return LONG2NUM(FIX2LONG(e) - 1);
 	    }

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/range/eql_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/range/eql_tags.txt	2010-05-08 01:30:20 UTC (rev 4048)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/range/eql_tags.txt	2010-05-08 02:20:37 UTC (rev 4049)
@@ -1 +0,0 @@
-fails:Range#eql? returns true for subclasses to Range

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/range/equal_value_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/range/equal_value_tags.txt	2010-05-08 01:30:20 UTC (rev 4048)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/range/equal_value_tags.txt	2010-05-08 02:20:37 UTC (rev 4049)
@@ -1 +0,0 @@
-fails:Range#== returns true for subclasses to Range

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/range/to_a_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/range/to_a_tags.txt	2010-05-08 01:30:20 UTC (rev 4048)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/range/to_a_tags.txt	2010-05-08 02:20:37 UTC (rev 4049)
@@ -1 +0,0 @@
-fails:Range#to_a works with Ranges of Symbols
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100507/5ece8395/attachment-0001.html>


More information about the macruby-changes mailing list