I am trying to compare two Ruby Hashes using the following code:
#!/usr/bin/env ruby
require "yaml"
require "active_support"
file1 = YAML::load(File.open('./en_20110207.yml'))
file2 = YAML::load(File.open('./locales/en.yml'))
arr = []
file1.select { |k,v|
file2.select { |k2, v2|
arr << "#{v2}" if "#{v}" != "#{v2}"
}
}
puts arr
The output to the screen is the full file from file2. I know for a fact that the files are different, but the script doesn’t seem to pick it up.
You can compare hashes directly for equality:
You can convert the hashes to arrays, then get their difference:
Simplifying further:
Assigning difference via a ternary structure:
Doing it all in one operation and getting rid of the
differencevariable: