[前][次][番号順一覧][スレッド一覧]

ruby-changes:12936

From: nobu <ko1@a...>
Date: Thu, 27 Aug 2009 00:00:18 +0900 (JST)
Subject: [ruby-changes:12936] Ruby:r24675 (trunk): * random.c (random_rand): refined error message.

nobu	2009-08-27 00:00:00 +0900 (Thu, 27 Aug 2009)

  New Revision: 24675

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24675

  Log:
    * random.c (random_rand): refined error message.

  Modified files:
    trunk/ChangeLog
    trunk/random.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24674)
+++ ChangeLog	(revision 24675)
@@ -1,5 +1,7 @@
-Wed Aug 26 23:58:39 2009  Nobuyoshi Nakada  <nobu@r...>
+Wed Aug 26 23:59:56 2009  Nobuyoshi Nakada  <nobu@r...>
 
+	* random.c (random_rand): refined error message.
+
 	* random.c (random_rand): fixed for edge cases of ranges.
 	  [ruby-dev:39166]
 
Index: random.c
===================================================================
--- random.c	(revision 24674)
+++ random.c	(revision 24675)
@@ -902,13 +902,6 @@
     return r;
 }
 
-static inline VALUE
-add_to_begin(VALUE beg, VALUE offset)
-{
-    if (beg == Qundef) return offset;
-    return rb_funcall2(beg, id_plus, 1, &offset);
-}
-
 static VALUE
 rand_int(struct MT *mt, VALUE vmax, int restrictive)
 {
@@ -1049,10 +1042,28 @@
 	}
     }
     else {
+	v = Qnil;
 	NUM2LONG(vmax);
     }
-    if (NIL_P(v)) rb_raise(rb_eArgError, "invalid argument");
-    return add_to_begin(beg, v);
+    if (NIL_P(v)) {
+	VALUE mesg = rb_str_new_cstr("invalid argument - ");
+	rb_str_append(mesg, rb_obj_as_string(argv[0]));
+	rb_exc_raise(rb_exc_new3(rb_eArgError, mesg));
+    }
+    if (beg == Qundef) return v;
+    if (FIXNUM_P(beg) && FIXNUM_P(v)) {
+	long x = FIX2LONG(beg) + FIX2LONG(v);
+	return LONG2NUM(x);
+    }
+    switch (BUILTIN_TYPE(v)) {
+      case T_BIGNUM:
+	return rb_big_plus(v, beg);
+      case T_FLOAT:
+	RFLOAT_VALUE(v) += RFLOAT_VALUE(rb_check_to_float(beg));
+	return v;
+      default:
+	return rb_funcall2(v, id_plus, 1, &beg);
+    }
 }
 
 /*

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]