Skip to content

Instantly share code, notes, and snippets.

@elyscape elyscape/upcase.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Feb 28, 2015

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

@elyscape

This comment has been minimized.

Copy link
Owner Author

commented Feb 28, 2015

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
You can’t perform that action at this time.