summaryrefslogtreecommitdiff
path: root/lib/rational.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-02-06 09:49:12 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-02-06 09:49:12 +0000
commit117b4df7c78ccb9437e2f14ecd2cd6f51cb757f3 (patch)
treeef293f2979213ad5d6c066ba6a32d2a385a74514 /lib/rational.rb
parent9a4c66277185ea3239f05704c881c72520a6da90 (diff)
* lib/complex.rb (Complex#==): should not raise error by type
mismatch. * lib/rational.rb (Rational#==): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3451 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rational.rb')
-rw-r--r--lib/rational.rb28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib/rational.rb b/lib/rational.rb
index 1e41b9a..e63e8c4 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