Last active
August 29, 2015 14:16
-
-
Save elyscape/b4f07dfc489c41247810 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deep_upcase(item) | |
if item.respond_to? :upcase | |
return item.upcase | |
end | |
case item | |
when Array | |
item.map { |i| deep_upcase(i) } | |
when Hash | |
Hash[item.map { |k,v| [deep_upcase(k), deep_upcase(v)] } ] | |
else | |
item | |
end | |
end | |
Puppet::Parser::Functions.newfunction(:upcase, :type => :rvalue) do |args| | |
deep_upcase(args[0]) | |
end |
The benefit to having the recursive function be a separate function is that then you can perform validation on the top-level object passed in and not on what's contained inside it, so upcase(4)
is invalid but upcase({'a' => 4})
would still work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Realistically we can just do the same pattern to upcase instead of creating a new function that goes further but like the recursion