Skip to content

Instantly share code, notes, and snippets.

@elyscape
Last active August 29, 2015 14:16
Show Gist options
  • Save elyscape/b4f07dfc489c41247810 to your computer and use it in GitHub Desktop.
Save elyscape/b4f07dfc489c41247810 to your computer and use it in GitHub Desktop.
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
@cyberious
Copy link

Realistically we can just do the same pattern to upcase instead of creating a new function that goes further but like the recursion

@elyscape
Copy link
Author

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