8000 * lib/complex.rb (Complex#==): should not raise error by type · documenting-ruby/ruby@117b4df · GitHub
[go: up one dir, main page]

Skip to content

Commit 117b4df

Browse files
author
matz
committed
* 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
1 parent 9a4c662 commit 117b4df

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Thu Feb 6 17:43:56 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
2+
3+
* lib/complex.rb (Complex#==): should not raise error by type
4+
mismatch.
5+
6+
* lib/rational.rb (Rational#==): ditto.
7+
18
Thu Feb 6 11:44:40 2003 MoonWolf <moonwolf@moonwolf.com>
29

310
* re.c (rb_reg_initialize_m): 3rd argument was ignored.

lib/complex.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ def == (other)
265265
elsif Complex.generic?(other)
266266
@real == other and @image == 0
267267
else
268-
x , y = other.coerce(self)
269-
x == y
268+
other == self
270269
end
271270
end
272271

lib/rational.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def + (a)
9292
elsif a.kind_of?(Float)
9393
Float(self) + a
9494
else
95-
x , y = a.coerce(self)
95+
x, y = a.coerce(self)
9696
x + y
9797
end
9898
end
@@ -107,7 +107,7 @@ def - (a)
107107
elsif a.kind_of?(Float)
108108
Float(self) - a
109109
else
110-
x , y = a.coerce(self)
110+
x, y = a.coerce(self)
111111
x - y
112112
end
113113
end
@@ -122,7 +122,7 @@ def * (a)
122122
elsif a.kind_of?(Float)
123123
Float(self) * a
124124
else
125-
x , y = a.coerce(self)
125+
x, y = a.coerce(self)
126126
x * y
127127
end
128128
end
@@ -138,7 +138,7 @@ def / (a)
138138
elsif a.kind_of?(Float)
139139
Float(self) / a
140140
else
141-
x , y = a.coerce(self)
141+
x, y = a.coerce(self)
142142
x / y
143143
end
144144
end
@@ -161,7 +161,7 @@ def ** (other)
161161
elsif other.kind_of?(Float)
162162
Float(self) ** other
163163
else
164-
x , y = other.coerce(self)
164+
x, y = other.coerce(self)
165165
x ** y
166166
end
167167
end
@@ -184,6 +184,18 @@ def abs
184184
end
185185
end
186186

187+
def == (other)
188+
if other.kind_of?(Rational)
189+
@numerator == other.numerator and @denominator == other.denominator
190+
elsif other.kind_of?(Integer)
191+
self == Rational.new!(other, 1)
192+
elsif other.kind_of?(Float)
193+
Float(self) == other
194+
else
195+
other == self
196+
end
197+
end
198+
187199
def <=> (other)
188200
if other.kind_of?(Rational)
189201
num = @numerator * other.denominator
@@ -200,9 +212,11 @@ def <=> (other)
200212
return self <=> Rational.new!(other, 1)
201213
elsif other.kind_of?(Float)
202214
return Float(self) <=> other
203-
else
204-
x , y = other.coerce(self)
215+
elsif defined? other.coerce
216+
x, y = other.coerce(self)
205217
return x <=> y
218+
else
219+
return nil
206220
end
207221
end
208222

0 commit comments

Comments
 (0)
0