About the behavior of ruby Hash # ==

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

About the behavior of ruby Hash # ==
About the [ruby] operator
About the handling of Null
About the description of Docker-compose.yml
[Technical memo] About the advantages and disadvantages of Ruby
About the basics of Android development
Extraction of "ruby" double hash * Review
[Ruby] See the essence of ArgumentError
[Ruby] Questions and verification about the number of method arguments
A note about the seed function of Ruby on Rails
[Ruby] About the difference between 2 dots and 3 dots of range object.
About the role of the initialize method
[Ruby] How to retrieve the contents of a double hash
Think about the 7 rules of Optional
[Ruby] Review about nesting of each
About the default behavior of decimal point rounding using java.text.NumberFormat
Explanation about Array object of Ruby
[Ruby] I want to reverse the order of the hash table
[Ruby] Display the contents of variables
Summary about the introduction of Device
About the log level of java.util.logging.Logger
About the behavior at the time of error of Files.copy (pathA, pathB)
About the version of Docker's Node.js image
What is testing? ・ About the importance of testing
part of the syntax of ruby ​​on rails
About the operation of next () and nextLine ()
[Ruby] Cut off the contents of twitter-ads
About the initial display of Spring Framework
Ruby from the perspective of other languages
About the error message Invalid redeclaration of'***'
About the treatment of BigDecimal (with reflection)
About the number of threads of Completable Future
About the mechanism of the Web and HTTP
Investigate the behavior of JPA transaction timeout
[Java] [Spring] Test the behavior of the logger
About Ruby symbols
About ruby ​​form
About Ruby Hashes
Ruby Hash notes
[Ruby] Hash retrieval
[About double hash]
Basics of Ruby
[Ruby] I want to extract only the value of the hash and only the key
About the method
[Ruby basics] About the role of true and break in the while statement
About Ruby arrays
[ruby] Double hash
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About the package
About Ruby variables
Ruby double hash
About Ruby methods
Behavior of ThreadPoolTaskExecutor
How to find the cause of the Ruby error
[Ruby] Summary of class definitions. Master the basics.
[Ruby] Behavior of evaluation of conditional expression in while
Think about the combination of Servlet and Ajax
About the official start guide of Spring Framework