W
walter
Hi everyone,
I am having difficulty with Hash and sometimes Arrays with ==. Below
is an example with Hashes. I am trying to do some simple cleanup of
our data and was trying TDD some code but I do not understand why it
says my 2 Hashes are different. They appear identical but show up as
false using ==.
According to ri :
----------------------------------------------------------------
Hash#==
hsh == anOtherHash -> true or false
----------------------------------------------------------------------
--
Equality---Two hashes are equal if they each contain the same
number of keys and if each key-value pair is equal to (according
to
Object#==) the corresponding elements in the other hash.
h1 = { "a" => 1, "c" => 2 }
h2 = { 7 => 35, "c" => 2, "a" => 1 }
h3 = { "a" => 1, "c" => 2, 7 => 35 }
h4 = { "a" => 1, "d" => 2, "f" => 35 }
h1 == h2 #=> false
h2 == h3 #=> true
h3 == h4 #=> false
This should work. The code is below along with the Test::Unit
output.
Am I missing something stupid????
<CODE>
module ShipToCleanUp
def notes
results = []
1.upto(6) do |i|
results << send("note#{i}")
end
results.join(" ").strip
end
def word_counts
results = Hash.new{|h,k| h[k] = 0}
notes.split(/\s+/).each{|word| results[word]+=1}
results
end
end
if __FILE__ == $0
require 'test/unit'
class ShipToCleanUpTester < Test::Unit::TestCase
class ShipTo
include ShipToCleanUp
attr_accessor :note1, :note2, :note3, :note4, :note5, :note6
def initialize(n1='', n2='', n3='', n4='', n5='', n6='')
@note1, @note2, @note3, @note4, @note5, @note6 = n1, n2, n3,
n4, n5, n6
end
end
def setup
@shipTo1 = ShipTo.new('LINE 1 AND SOME MORE ON LINE 1 ', '
***** LINE 2 ***** ', '', '', 'LINE 5 ', 'AND LINE 6')
end
def test_word_counts
expected = {}
expected['LINE'] = 5
expected['1'] = 2
expected['AND'] = 2
expected['SOME'] = 1
expected['MORE'] = 1
expected['ON'] = 1
expected['*****'] = 2
expected['2'] = 1
expected['5'] = 1
expected['6'] = 1
assert_equal(expected, @shipTo1.word_counts)
end
def test_notes_some_empty
expected = "1 6"
assert_equal(expected, ShipTo.new("1", '', '', '', '',
'6').notes)
end
def test_notes_empty
expected = ""
assert_equal(expected, ShipTo.new.notes)
end
def test_notes
expected = "LINE 1 AND SOME MORE ON LINE 1 ***** LINE 2
***** LINE 5 AND LINE 6"
assert_equal(expected, @shipTo1.notes)
end
end
end
</CODE>
When I run the code I get
Loaded suite C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/rbC.tmp
Started
...F
Finished in 0.04 seconds.
1) Failure:
test_word_counts(ShipToCleanUpTester)
[C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/rbC.tmp:46]:
<{"6"=>1,
"AND"=>2,
"LINE"=>5,
"*****"=>2,
"1"=>2,
"2"=>1,
"ON"=>1,
"SOME"=>1,
"5"=>1,
"MORE"=>1}> expected but was
<{"6"=>1,
"AND"=>2,
"LINE"=>5,
"*****"=>2,
"1"=>2,
"2"=>1,
"ON"=>1,
"SOME"=>1,
"5"=>1,
"MORE"=>1}>.
4 tests, 4 assertions, 1 failures, 0 errors
Any insight would be greatly appreciated.
Thanks,
Walt
*****************************************************
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : (e-mail address removed)
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
*****************************************************
I am having difficulty with Hash and sometimes Arrays with ==. Below
is an example with Hashes. I am trying to do some simple cleanup of
our data and was trying TDD some code but I do not understand why it
says my 2 Hashes are different. They appear identical but show up as
false using ==.
According to ri :
----------------------------------------------------------------
Hash#==
hsh == anOtherHash -> true or false
----------------------------------------------------------------------
--
Equality---Two hashes are equal if they each contain the same
number of keys and if each key-value pair is equal to (according
to
Object#==) the corresponding elements in the other hash.
h1 = { "a" => 1, "c" => 2 }
h2 = { 7 => 35, "c" => 2, "a" => 1 }
h3 = { "a" => 1, "c" => 2, 7 => 35 }
h4 = { "a" => 1, "d" => 2, "f" => 35 }
h1 == h2 #=> false
h2 == h3 #=> true
h3 == h4 #=> false
This should work. The code is below along with the Test::Unit
output.
Am I missing something stupid????
<CODE>
module ShipToCleanUp
def notes
results = []
1.upto(6) do |i|
results << send("note#{i}")
end
results.join(" ").strip
end
def word_counts
results = Hash.new{|h,k| h[k] = 0}
notes.split(/\s+/).each{|word| results[word]+=1}
results
end
end
if __FILE__ == $0
require 'test/unit'
class ShipToCleanUpTester < Test::Unit::TestCase
class ShipTo
include ShipToCleanUp
attr_accessor :note1, :note2, :note3, :note4, :note5, :note6
def initialize(n1='', n2='', n3='', n4='', n5='', n6='')
@note1, @note2, @note3, @note4, @note5, @note6 = n1, n2, n3,
n4, n5, n6
end
end
def setup
@shipTo1 = ShipTo.new('LINE 1 AND SOME MORE ON LINE 1 ', '
***** LINE 2 ***** ', '', '', 'LINE 5 ', 'AND LINE 6')
end
def test_word_counts
expected = {}
expected['LINE'] = 5
expected['1'] = 2
expected['AND'] = 2
expected['SOME'] = 1
expected['MORE'] = 1
expected['ON'] = 1
expected['*****'] = 2
expected['2'] = 1
expected['5'] = 1
expected['6'] = 1
assert_equal(expected, @shipTo1.word_counts)
end
def test_notes_some_empty
expected = "1 6"
assert_equal(expected, ShipTo.new("1", '', '', '', '',
'6').notes)
end
def test_notes_empty
expected = ""
assert_equal(expected, ShipTo.new.notes)
end
def test_notes
expected = "LINE 1 AND SOME MORE ON LINE 1 ***** LINE 2
***** LINE 5 AND LINE 6"
assert_equal(expected, @shipTo1.notes)
end
end
end
</CODE>
When I run the code I get
Loaded suite C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/rbC.tmp
Started
...F
Finished in 0.04 seconds.
1) Failure:
test_word_counts(ShipToCleanUpTester)
[C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/rbC.tmp:46]:
<{"6"=>1,
"AND"=>2,
"LINE"=>5,
"*****"=>2,
"1"=>2,
"2"=>1,
"ON"=>1,
"SOME"=>1,
"5"=>1,
"MORE"=>1}> expected but was
<{"6"=>1,
"AND"=>2,
"LINE"=>5,
"*****"=>2,
"1"=>2,
"2"=>1,
"ON"=>1,
"SOME"=>1,
"5"=>1,
"MORE"=>1}>.
4 tests, 4 assertions, 1 failures, 0 errors
Any insight would be greatly appreciated.
Thanks,
Walt
*****************************************************
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : (e-mail address removed)
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
*****************************************************