Tampa
Tampa

Reputation: 78402

Chef and ruby: how to convert a array into a set

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

Answers (2)

Kokizzu
Kokizzu

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

steenslag
steenslag

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

Related Questions