Skip to content

Instantly share code, notes, and snippets.

@mrhead
Last active July 29, 2020 07:41
Show Gist options
  • Save mrhead/93dc06644a541740a23d74f2f2fbc61d to your computer and use it in GitHub Desktop.
Save mrhead/93dc06644a541740a23d74f2f2fbc61d to your computer and use it in GitHub Desktop.
# Count number of words in a locale file.
def word_count(hash)
hash.values.sum do |value|
if value.is_a?(Hash)
word_count(value)
else
value.split.size
end
end
end
# require "yaml"
# hash = YAML.load(File.read("config/locales/en.yml"))
# puts word_count(hash)
require 'minitest/autorun'
class Tests < MiniTest::Unit::TestCase
def test_1
hash = {
a: "one two"
}
assert_equal 2, word_count(hash)
end
def test_2
hash = {
a: "one two",
b: {
c: "three four",
d: "five"
}
}
assert_equal 5, word_count(hash)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment