How far do you compare Hash with ruby? I was curious, so I tried to verify it.
As a result, although it is described in the official document ** Returns true if self and other hold the same number of keys, the keys are all equal compared by the eql? method, and the values are all equal compared by the == method. ** **
Reference: instance method Hash # ==
It seems to work, let's verify.
require 'securerandom'
keys = Array.new
values = Array.new
#Create 1000 test data
1000.times do
keys << SecureRandom.alphanumeric
end
1000.times do
values << SecureRandom.base64(100)
end
#Comparison of different contents of value
a = Hash.new
b = Hash.new
keys.each do |key|
a[key] = values.sample
b[key] = values.sample
end
puts a == b ? "match!!" : "unmatch..."
#Comparison with the same contents of value
a = Hash.new
b = Hash.new
keys.each_with_index do |key, i|
a[key] = values[i]
b[key] = values[i]
end
puts a == b ? "match!!" : "unmatch..."
result
$ >> ruby test.rb
unmatch...
match!!
It seems that it is being compared properly, as it is officially written.
Recommended Posts