A
andrew.oxenburgh
Hi,
I've just started with Ruby from Java, and am having trouble getting a
set to recognise equivalence between objects. Basically it is adding
multiple equal objects to the set.
I have created a class and overridden all of the operators <=> , ==,
<, >, eqls?, equals?, hash to indicate that it should use an internal
variable to compare.
Please find enclosed a unit test, the object, and the output from the
test. Cool language BTW. Really enjoying it up until this moment. I'm
probably missing some subtlety of the language.
Any help greatly appreciated.
-------- source starts --------
require "test/unit"
require "set"
class JunkTest < Test::Unit::TestCase
def test_arraysMuckingAroundWith
a = Thingy.new(1)
b = Thingy.new(2)
c = Thingy.new(1)
d=Set.new
d << a
d << b
d << c
puts "output set, with duplicates - the first 2 object should
be equivilent" + d.sort.to_s
end
end
class Thingy
attr_accessor :value
def <=> (thing)
self.value <=> thing.value
end
def == (thing)
self.value == thing.value
end
def > (thing)
self.value > thing.value
end
def < (thing)
self.value < thing.value
end
def eqls? (thing)
self.value.eqls?(thing.value)
end
def equals? (thing)
self.value.equals?(thing.value)
end
def initialize(val)
@value = val
end
def hash
puts "hash " + @value.hash.to_s
@value.hash
end
def to_s
value.to_s + ", "
end
end
-------- source ends --------
-------- output starts --------
C:\dev\ruby\bin\ruby.exe -e
"STDOUT.sync=true;STDERR.sync=true;load($0=ARGV.shift)" C:/projects/
SearchJars/test/JunkTest.rb --name test_arraysMuckingAroundWith
Loaded suite C:/projects/SearchJars/test/JunkTest
Started
hash 3
hash 5
hash 3
output set, with duplicates - 1, 1, 2,
..
Finished in 0.0 seconds.
1 tests, 0 assertions, 0 failures, 0 errors
Process finished with exit code 0
-------- output ends --------
Regards
Andrew Oxenburgh
I've just started with Ruby from Java, and am having trouble getting a
set to recognise equivalence between objects. Basically it is adding
multiple equal objects to the set.
I have created a class and overridden all of the operators <=> , ==,
<, >, eqls?, equals?, hash to indicate that it should use an internal
variable to compare.
Please find enclosed a unit test, the object, and the output from the
test. Cool language BTW. Really enjoying it up until this moment. I'm
probably missing some subtlety of the language.
Any help greatly appreciated.
-------- source starts --------
require "test/unit"
require "set"
class JunkTest < Test::Unit::TestCase
def test_arraysMuckingAroundWith
a = Thingy.new(1)
b = Thingy.new(2)
c = Thingy.new(1)
d=Set.new
d << a
d << b
d << c
puts "output set, with duplicates - the first 2 object should
be equivilent" + d.sort.to_s
end
end
class Thingy
attr_accessor :value
def <=> (thing)
self.value <=> thing.value
end
def == (thing)
self.value == thing.value
end
def > (thing)
self.value > thing.value
end
def < (thing)
self.value < thing.value
end
def eqls? (thing)
self.value.eqls?(thing.value)
end
def equals? (thing)
self.value.equals?(thing.value)
end
def initialize(val)
@value = val
end
def hash
puts "hash " + @value.hash.to_s
@value.hash
end
def to_s
value.to_s + ", "
end
end
-------- source ends --------
-------- output starts --------
C:\dev\ruby\bin\ruby.exe -e
"STDOUT.sync=true;STDERR.sync=true;load($0=ARGV.shift)" C:/projects/
SearchJars/test/JunkTest.rb --name test_arraysMuckingAroundWith
Loaded suite C:/projects/SearchJars/test/JunkTest
Started
hash 3
hash 5
hash 3
output set, with duplicates - 1, 1, 2,
..
Finished in 0.0 seconds.
1 tests, 0 assertions, 0 failures, 0 errors
Process finished with exit code 0
-------- output ends --------
Regards
Andrew Oxenburgh