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

ruby-changes:13319

From: marcandre <ko1@a...>
Date: Fri, 25 Sep 2009 04:34:40 +0900 (JST)
Subject: [ruby-changes:13319] Ruby:r25082 (ruby_1_8): * lib/rational.rb (#+, #-, #/, #**, #<=>): Return correct error message in case coercion fails. Based on a patch by Run Paint Run Run

marcandre	2009-09-25 04:34:27 +0900 (Fri, 25 Sep 2009)

  New Revision: 25082

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

  Log:
    * lib/rational.rb (#+, #-, #/, #**, #<=>): Return correct error message in case coercion fails. Based on a patch by Run Paint Run Run [ruby-core:23903]

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/lib/rational.rb

Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 25081)
+++ ruby_1_8/ChangeLog	(revision 25082)
@@ -1,3 +1,9 @@
+Fri Sep 25 04:34:08 2009  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/rational.rb (#+, #-, #/, #**, #<=>): Return correct error
+	  message in case coercion fails. Based on a patch by Run Paint Run Run
+	  [ruby-core:23903]
+
 Thu Sep 24 15:52:34 2009  NAKAMURA Usaku  <usa@r...>
 
 	* instruby.rb: win32/win32.h exists in srcdir.
Index: ruby_1_8/lib/rational.rb
===================================================================
--- ruby_1_8/lib/rational.rb	(revision 25081)
+++ ruby_1_8/lib/rational.rb	(revision 25082)
@@ -131,7 +131,7 @@
     elsif a.kind_of?(Float)
       Float(self) + a
     else
-      x, y = a.coerce(self)
+      x, y = a.coerce(self) rescue raise TypeError, "#{a.class} can't be coerced into #{self.class}"
       x + y
     end
   end
@@ -155,7 +155,7 @@
     elsif a.kind_of?(Float)
       Float(self) - a
     else
-      x, y = a.coerce(self)
+      x, y = a.coerce(self) rescue raise TypeError, "#{a.class} can't be coerced into #{self.class}"
       x - y
     end
   end
@@ -180,7 +180,7 @@
     elsif a.kind_of?(Float)
       Float(self) * a
     else
-      x, y = a.coerce(self)
+      x, y = a.coerce(self) rescue raise TypeError, "#{a.class} can't be coerced into #{self.class}"
       x * y
     end
   end
@@ -203,7 +203,7 @@
     elsif a.kind_of?(Float)
       Float(self) / a
     else
-      x, y = a.coerce(self)
+      x, y = a.coerce(self) rescue raise TypeError, "#{a.class} can't be coerced into #{self.class}"
       x / y
     end
   end
@@ -235,7 +235,7 @@
     elsif other.kind_of?(Float)
       Float(self) ** other
     else
-      x, y = other.coerce(self)
+      x, y = other.coerce(self) rescue raise TypeError, "#{a.class} can't be coerced into #{self.class}"
       x ** y
     end
   end
@@ -322,11 +322,9 @@
       return self <=> Rational.new!(other, 1)
     elsif other.kind_of?(Float)
       return Float(self) <=> other
-    elsif defined? other.coerce
-      x, y = other.coerce(self)
-      return x <=> y
     else
-      return nil
+      x, y = other.coerce(self) rescue return nil
+      return x <=> y
     end
   end
 

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

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