summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--lib/complex.rb3
-rw-r--r--lib/rational.rb28
3 files changed, 29 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index b088bfa725..26d2d10e2b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Thu Feb 6 17:43:56 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * lib/complex.rb (Complex#==): should not raise error by type
+ mismatch.
+
+ * lib/rational.rb (Rational#==): ditto.
+
Thu Feb 6 11:44:40 2003 MoonWolf <moonwolf@moonwolf.com>
* re.c (rb_reg_initialize_m): 3rd argument was ignored.
diff --git a/lib/complex.rb b/lib/complex.rb
index ccf357c7a2..98af310d3e 100644
--- a/lib/complex.rb
+++ b/lib/complex.rb
@@ -265,8 +265,7 @@ class Complex < Numeric
elsif Complex.generic?(other)
@real == other and @image == 0
else
- x , y = other.coerce(self)
- x == y
+ other == self
end
end
diff --git a/lib/rational.rb b/lib/rational.rb
index 1e41b9a28a..e63e8c4814 100644
--- a/lib/rational.rb
+++ b/lib/rational.rb
@@ -92,7 +92,7 @@ class Rational < Numeric
elsif a.kind_of?(Float)
Float(self) + a
else
- x , y = a.coerce(self)
+ x, y = a.coerce(self)
x + y
end
end
@@ -107,7 +107,7 @@ class Rational < Numeric
elsif a.kind_of?(Float)
Float(self) - a
else
- x , y = a.coerce(self)
+ x, y = a.coerce(self)
x - y
end
end
@@ -122,7 +122,7 @@ class Rational < Numeric
elsif a.kind_of?(Float)
Float(self) * a
else
- x , y = a.coerce(self)
+ x, y = a.coerce(self)
x * y
end
end
@@ -138,7 +138,7 @@ class Rational < Numeric
elsif a.kind_of?(Float)
Float(self) / a
else
- x , y = a.coerce(self)
+ x, y = a.coerce(self)
x / y
end
end
@@ -161,7 +161,7 @@ class Rational < Numeric
elsif other.kind_of?(Float)
Float(self) ** other
else
- x , y = other.coerce(self)
+ x, y = other.coerce(self)
x ** y
end
end
@@ -184,6 +184,18 @@ class Rational < Numeric
end
end
+ def == (other)
+ if other.kind_of?(Rational)
+ @numerator == other.numerator and @denominator == other.denominator
+ elsif other.kind_of?(Integer)
+ self == Rational.new!(other, 1)
+ elsif other.kind_of?(Float)
+ Float(self) == other
+ else
+ other == self
+ end
+ end
+
def <=> (other)
if other.kind_of?(Rational)
num = @numerator * other.denominator
@@ -200,9 +212,11 @@ class Rational < Numeric
return self <=> Rational.new!(other, 1)
elsif other.kind_of?(Float)
return Float(self) <=> other
- else
- x , y = other.coerce(self)
+ elsif defined? other.coerce
+ x, y = other.coerce(self)
return x <=> y
+ else
+ return nil
end
end