13

我有一个看起来像这样的数组:

nodes = ['server1','server1','server2']

在厨师食谱中,我需要在传递给模板 erb 之前转换为一组。我怎么做?

4

2 回答 2

21

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"}>
于 2013-03-26T23:38:07.313 回答
10

如果你想让它唯一(因为一个集合是唯一的)但仍然是一个数组,你可以使用 |[]

nodes = ['server1','server1','server2']
nodes|[] 
# or nodes |= [] # for inplace operation

# => ["server1", "server2" ]
于 2013-03-26T23:49:23.627 回答