class Array

Public Instance Methods

combination(num) click to toggle source

Ruby < 1.8.7 doesn’t have this method but we use it in tests

# File lib/puppet/util/monkey_patches.rb, line 109
def combination(num)
  return [] if num < 0 || num > size
  return [[]] if num == 0
  return map{|e| [e] } if num == 1
  tmp = self.dup
  self[0, size - (num - 1)].inject([]) do |ret, e|
    tmp.shift
    ret += tmp.combination(num - 1).map{|a| a.unshift(e) }
  end
end
drop(n) click to toggle source

Ruby 1.8.5 lacks `drop`, which we don’t want to lose.

# File lib/puppet/util/monkey_patches.rb, line 123
def drop(n)
  n = n.to_int
  raise ArgumentError, "attempt to drop negative size" if n < 0

  slice(n, length - n) or []
end
to_zaml(z) click to toggle source
# File lib/puppet/util/zaml.rb, line 361
def to_zaml(z)
  z.first_time_only(self) {
    z.nested {
      if empty?
        z.emit('[]')
      else
        each { |v| z.nl('- '); v.to_zaml(z) }
      end
    }
  }
end