Reputation: 78402
I have an array that looks like this:
nodes = ['server1','server1','server2']
In a chef recipe I need to convert into a set before I pass to a template erb. How do I do that?
Upvotes: 17
Views: 20210
Reputation: 26898
if you want to make it unique (as a set is unique) but still as an array, you can use |[]
nodes = ['server1','server1','server2']
nodes|[]
# or nodes |= [] # for inplace operation
# => ["server1", "server2" ]
Upvotes: 14
Reputation: 80085
This pattern works with Set, Matrix, JSON etc.; it is the first thing to try.
require 'set'
nodes = ['server1','server1','server2']
p nodes.to_set # #<Set: {"server1", "server2"}>
Upvotes: 27