Maybe I’m doing something very stupid, but consider this.
The following little program:
class Awk < String
include Comparable
def <=>(other)
puts "I am called"
super
end
end
a = Awk.new(‘a’)
b = Awk.new(‘a’)
if a == b
puts 'Equal’
end
if a < b
puts 'Hurrah’
end
produces, quite against my expectation,
Equal
I am called
Which means that Awk#<=> isn’t called for ==
Both on 1.6.8 and 1.8.0
This must be my mistake, but I just don’t see it.
Suggestions ?
Han Holl
ts1
(ts)
27 March 2003 14:52
2
class Awk < String
include Comparable
pigeon% ruby -e 'p String.ancestors'
[String, Enumerable, Comparable, Object, Kernel]
pigeon%
This mean that your include is useless. The method String#== is
defined and ruby will use this method rather than use Awk#<=>
Guy Decoux
Robert
(Robert)
27 March 2003 16:54
3
“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200303271449.h2REnEx07474@moulon.inra.fr …
class Awk < String
include Comparable
pigeon% ruby -e ‘p String.ancestors’
[String, Enumerable, Comparable, Object, Kernel]
pigeon%
This mean that your include is useless. The method String#== is
defined and ruby will use this method rather than use Awk#<=>
The output would not change even if method <=> was invoked since
a = Awk.new(‘a’)
b = Awk.new(‘a’)
They are equal, although not identical.
robert
ts1
(ts)
27 March 2003 17:02
4
Well I must have some problems with english
The output would not change even if method <=> was invoked since
The initial output is
produces, quite against my expectation,
Equal
I am called
a = Awk.new('a')
b = Awk.new('a')
They *are* equal, although not identical.
Now I'll try to call <=>
pigeon% cat b.rb
#!/usr/bin/ruby
class Awk < String
def ==(other)
(self <=> other) == 0
end
def <=>(other)
puts "I am called"
super
end
end
a = Awk.new('a')
b = Awk.new('a')
if a == b
puts 'Equal'
end
if a < b
puts 'Hurrah'
end
pigeon%
pigeon% b.rb
I am called
Equal
I am called
pigeon%
well, I can see a difference :-))
Guy Decoux
Robert
(Robert)
28 March 2003 13:19
5
“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200303271659.h2RGxCb10204@moulon.inra.fr …
Well I must have some problems with english
In this case I’d say, it’s more likely that I have problems with english.
(see below)
code example
well, I can see a difference :-))
Sorry, I was on the wrong track: I assumed that the problem was in the
outcome - not in the methods called. I thought the expectation was not to
see “Equal” printed, while it was that method <=> be called by ==.
Regards
robert