Skip to content

Instantly share code, notes, and snippets.

@manchunlam
Last active September 28, 2015 04:38
Show Gist options
  • Save manchunlam/1385867 to your computer and use it in GitHub Desktop.
Save manchunlam/1385867 to your computer and use it in GitHub Desktop.
Change en.yml to Zoidberg's language
test_subject = {
abc: "foo",
bar: {
fox: { we: "es" }
},
foo: {
foobar: {
barfoo: "en",
usa: "f yeah",
uk: { queen: "save" }
}
}
}
puts "---------- test_subject is #{test_subject.inspect}"
# Joe: half-ass recursion
def change_value(a_hash)
something = a_hash.inject({}) {|result, (k, v)|
if v.is_a?(Hash)
result[k] = change_value(v)
else
result[k] = "zzz" # terminating condition
end
result
}
something
end
# Dylon: CS perfect recursion
def real_recursion(a, h)
if h.empty? # at the lowest point of tree
a
else
k, v = h.first
h.delete(k)
if v.is_a?(Hash)
a[k] = real_recursion({}, v)
else
a[k] = 'zzz' # terminating condition
end
real_recursion(a, h) # I need it to move to the next key of the same level
end
end
puts "---------- change_value(test_subject) is #{change_value(test_subject).inspect}"
puts "---------- real_recursion(test_subject) is #{real_recursion({}, test_subject).inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment