L
Li Chen
Hello everyone,
I have two hashes. They have the same keys but different or the same
values.
I want to loop through one hash and print out the values in both
hashes for the same key.I add a method to class Hash. I run the script
and it prints out the expected results. I wonder if I have to use
self#method in my implement.
Thanks for all the comments.
Li
class Hash
def self.my_each(hash1={}, hash2={})
hash1.each do |k,v|
if hash2.has_key?(k)
print k,"\t",v,"\t", hash2[k],"\n"
else
print k,"\t",v,"\n"
end
end
end
end
##############main#############
hash1={
'ATC'=>1,
'CTA'=>2,
'CAT'=>10,
'CCC'=>1
}
hash2={
'ATC'=>3,
'CTA'=>2,
'CAT'=>11
}
Hash.my_each(hash1, hash2)
I have two hashes. They have the same keys but different or the same
values.
I want to loop through one hash and print out the values in both
hashes for the same key.I add a method to class Hash. I run the script
and it prints out the expected results. I wonder if I have to use
self#method in my implement.
Thanks for all the comments.
Li
class Hash
def self.my_each(hash1={}, hash2={})
hash1.each do |k,v|
if hash2.has_key?(k)
print k,"\t",v,"\t", hash2[k],"\n"
else
print k,"\t",v,"\n"
end
end
end
end
##############main#############
hash1={
'ATC'=>1,
'CTA'=>2,
'CAT'=>10,
'CCC'=>1
}
hash2={
'ATC'=>3,
'CTA'=>2,
'CAT'=>11
}
Hash.my_each(hash1, hash2)