我有一个erb模板,可以为httpd生成配置文件。
最后写一个特定的位置很重要(这是一个全部的记录)
目前代码似乎是
cluster.apps.each do |app|
# Render config
end
我想重载apps对象上的每个方法以保证顺序。什么是开始寻找如何做到这一点的最佳地点?
答案 0 :(得分:4)
如果你想重载它,你可以做类似的事情
class Cluster
#..code
def each_application
return unless block_given? #ensure a block was given
a = @apps.shift #Implement this to grab the element you want
@apps.each{|x| yield x}
yield a #yield the element that you want last
end
end
所以你现在可以做到:
cluster.each_application do |app|
#Render config
end
使用上面的当前实现,它将连续产生所有元素(第一个除外)。最后一个产生的项目是第一个被移除的项目。