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

ruby-changes:26926

From: marcandre <ko1@a...>
Date: Wed, 30 Jan 2013 07:04:23 +0900 (JST)
Subject: [ruby-changes:26926] marcandRe: r38979 (trunk): * object.c: Improve error for failed implicit conversions [Bug #7539]

marcandre	2013-01-30 07:00:58 +0900 (Wed, 30 Jan 2013)

  New Revision: 38979

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

  Log:
    * object.c: Improve error for failed implicit conversions [Bug #7539]
    
    * error.c: Adapt rdoc
    
    * test/ruby/test_object.rb: Test for above

  Modified files:
    trunk/ChangeLog
    trunk/error.c
    trunk/object.c
    trunk/test/ruby/test_object.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38978)
+++ ChangeLog	(revision 38979)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jan 30 07:00:16 2013  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* object.c: Improve error for failed implicit conversions [Bug #7539]
+
+	* error.c: Adapt rdoc
+
+	* test/ruby/test_object.rb: Test for above
+
 Tue Jan 29 21:40:12 2013  Tanaka Akira  <akr@f...>
 
 	* lib/net/http/generic_request.rb (encode_multipart_form_data): remove
Index: object.c
===================================================================
--- object.c	(revision 38978)
+++ object.c	(revision 38979)
@@ -2277,6 +2277,7 @@ static struct conv_method_tbl { https://github.com/ruby/ruby/blob/trunk/object.c#L2277
     {"to_s", 0},
     {NULL, 0}
 };
+#define IMPLICIT_CONVERSIONS 7
 
 static VALUE
 convert_type(VALUE val, const char *tname, const char *method, int raise)
@@ -2296,7 +2297,9 @@ convert_type(VALUE val, const char *tnam https://github.com/ruby/ruby/blob/trunk/object.c#L2297
     r = rb_check_funcall(val, m, 0, 0);
     if (r == Qundef) {
 	if (raise) {
-	    rb_raise(rb_eTypeError, "can't convert %s into %s",
+	    rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS
+                ? "no implicit conversion of %s into %s"
+                : "can't convert %s into %s",
 		     NIL_P(val) ? "nil" :
 		     val == Qtrue ? "true" :
 		     val == Qfalse ? "false" :
Index: error.c
===================================================================
--- error.c	(revision 38978)
+++ error.c	(revision 38979)
@@ -1390,7 +1390,7 @@ syserr_eqq(VALUE self, VALUE exc) https://github.com/ruby/ruby/blob/trunk/error.c#L1390
  *
  *  <em>raises the exception:</em>
  *
- *     TypeError: can't convert String into Integer
+ *     TypeError: no implicit conversion of String into Integer
  *
  */
 
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 38978)
+++ test/ruby/test_object.rb	(revision 38979)
@@ -885,4 +885,12 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L885
     assert_not_initialize_copy {st.new}
     assert_not_initialize_copy {Time.now}
   end
+
+  def test_type_error_message
+    issue = "Bug #7539"
+    err = assert_raise(TypeError){ Integer([42]) }
+    assert_equal "can't convert Array into Integer", err.message, issue
+    err = assert_raise(TypeError){ [].first([42]) }
+    assert_equal 'no implicit conversion of Array into Integer', err.message, issue
+  end
 end

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

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