mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
9a4c662771
commit
117b4df7c7
3 changed files with 29 additions and 9 deletions
|
@ -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>
|
Thu Feb 6 11:44:40 2003 MoonWolf <moonwolf@moonwolf.com>
|
||||||
|
|
||||||
* re.c (rb_reg_initialize_m): 3rd argument was ignored.
|
* re.c (rb_reg_initialize_m): 3rd argument was ignored.
|
||||||
|
|
|
@ -265,8 +265,7 @@ class Complex < Numeric
|
||||||
elsif Complex.generic?(other)
|
elsif Complex.generic?(other)
|
||||||
@real == other and @image == 0
|
@real == other and @image == 0
|
||||||
else
|
else
|
||||||
x , y = other.coerce(self)
|
other == self
|
||||||
x == y
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ class Rational < Numeric
|
||||||
elsif a.kind_of?(Float)
|
elsif a.kind_of?(Float)
|
||||||
Float(self) + a
|
Float(self) + a
|
||||||
else
|
else
|
||||||
x , y = a.coerce(self)
|
x, y = a.coerce(self)
|
||||||
x + y
|
x + y
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -107,7 +107,7 @@ class Rational < Numeric
|
||||||
elsif a.kind_of?(Float)
|
elsif a.kind_of?(Float)
|
||||||
Float(self) - a
|
Float(self) - a
|
||||||
else
|
else
|
||||||
x , y = a.coerce(self)
|
x, y = a.coerce(self)
|
||||||
x - y
|
x - y
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -122,7 +122,7 @@ class Rational < Numeric
|
||||||
elsif a.kind_of?(Float)
|
elsif a.kind_of?(Float)
|
||||||
Float(self) * a
|
Float(self) * a
|
||||||
else
|
else
|
||||||
x , y = a.coerce(self)
|
x, y = a.coerce(self)
|
||||||
x * y
|
x * y
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -138,7 +138,7 @@ class Rational < Numeric
|
||||||
elsif a.kind_of?(Float)
|
elsif a.kind_of?(Float)
|
||||||
Float(self) / a
|
Float(self) / a
|
||||||
else
|
else
|
||||||
x , y = a.coerce(self)
|
x, y = a.coerce(self)
|
||||||
x / y
|
x / y
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -161,7 +161,7 @@ class Rational < Numeric
|
||||||
elsif other.kind_of?(Float)
|
elsif other.kind_of?(Float)
|
||||||
Float(self) ** other
|
Float(self) ** other
|
||||||
else
|
else
|
||||||
x , y = other.coerce(self)
|
x, y = other.coerce(self)
|
||||||
x ** y
|
x ** y
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -184,6 +184,18 @@ class Rational < Numeric
|
||||||
end
|
end
|
||||||
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)
|
def <=> (other)
|
||||||
if other.kind_of?(Rational)
|
if other.kind_of?(Rational)
|
||||||
num = @numerator * other.denominator
|
num = @numerator * other.denominator
|
||||||
|
@ -200,9 +212,11 @@ class Rational < Numeric
|
||||||
return self <=> Rational.new!(other, 1)
|
return self <=> Rational.new!(other, 1)
|
||||||
elsif other.kind_of?(Float)
|
elsif other.kind_of?(Float)
|
||||||
return Float(self) <=> other
|
return Float(self) <=> other
|
||||||
else
|
elsif defined? other.coerce
|
||||||
x , y = other.coerce(self)
|
x, y = other.coerce(self)
|
||||||
return x <=> y
|
return x <=> y
|
||||||
|
else
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue