Skip to content

Instantly share code, notes, and snippets.

@kyamaguchi
Created May 22, 2012 04:58
Show Gist options
  • Save kyamaguchi/2766718 to your computer and use it in GitHub Desktop.
Save kyamaguchi/2766718 to your computer and use it in GitHub Desktop.
Collect values from deep hash
# http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-values
# http://rxr.whitequark.org/rubinius/source/kernel/common/hash18.rb
class Hash
def deep_values
ary = []
each_value do |value|
if value.is_a? Hash
ary += value.deep_values
elsif value.is_a? Array
ary += value
else
ary << value
end
end
ary
end
end
# For checking of i18n yaml files.
# data = YAML.load(File.open("config/locales/ja.yml").read)
# words = data.deep_values.compact.map(&:to_s).reject{|v| v =~ /^\s*$/ }.sort
# puts words.inspect
# words.each do |word|
# words.each do |another|
# next if word == another
# if another.index(word)
# puts "[#{word}] is included in [#{another}]."
# end
# end
# end
# words.each_with_index do |word,i|
# words.each_with_index do |another,j|
# next if i == j
# puts "DUPLICATE [#{word}]" if word == another
# puts "[#{word}] is included in [#{another}]." if another.index(word)
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment