I’ve create a custom type in Puppet (simplified for this example). If I use it like this (two items in collections)…
my_type { "example1":
ensure => present,
collections => ["abc", "def"]
}
…in my provider, resource[:collections] is of type Array. That is good, and right.
But if collections contains only 1 item…
my_type { "example2":
ensure => present,
collections => ["abc"],
}
…resource[:collections] is a String, which is most disconcerting, and a pain in the ass to deal with.
Is this a Ruby thing, a Puppet thing (I’m new to both) or just some cosmic wrinkle in the coding universe I’ve stumbled upon? And more importantly, is there a workaround? Or am I just plain doing it wrong? I’ve been told that before. Don’t hold back.
While I can’t tell you why this happens, the standard workaround for dealing with things that can be either arrays or single objects is using the splat operator like this:
[*foo]. In casefoowas an array its elements will be “exploded” into a new one, so you still have an array. Iffoowas just a plain object, you now have a one element array.