我有一个看起来像这样的数组:
nodes = ['server1','server1','server2']
在厨师食谱中,我需要在传递给模板 erb 之前转换为一组。我怎么做?
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"}>
如果你想让它唯一(因为一个集合是唯一的)但仍然是一个数组,你可以使用 |[]
nodes = ['server1','server1','server2']
nodes|[]
# or nodes |= [] # for inplace operation
# => ["server1", "server2" ]