Sha256: e63d0d2889726d9ea086fff837d66592a77495fa39700810d1d529d7fae9f2d4

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module BBLib

  # Takes two arrays (can be of different length) and interleaves them like [a[0], b[0], a[1], b[1]...]
  def self.interleave a, b, filler: nil
    if a.size < b.size
      a = a.dup
      while a.size < b.size
        a.push filler
      end
    end
    a.zip(b).flatten(1)
  end

end

class Array
  def msplit *delims, keep_empty: false
    self.map{ |i| i.msplit(delims, keep_empty:keep_empty)}.flatten
  end

  def keys_to_sym clean: false
    self.map{ |v| Hash === v || Array === v ? v.keys_to_sym(clean:clean) : v }
  end

  def keys_to_s clean: false
    self.map{ |v| Hash === v || Array === v ? v.keys_to_s : v }
  end

  def to_xml level: 0, key:nil
    map do |v|
      nested = v.respond_to?(:to_xml)
      value = nested ? v.to_xml(level:level+1, key:key) : v
      "\t"*level + "<#{key}>\n" + (nested ? '' : "\t"*(level+1)) + "#{value}\n" + "\t"*level + "</#{key}>\n"
    end.join
  end

  def interleave b, filler: nil
    BBLib.interleave self, b, filler: filler
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bblib-0.2.2 lib/array/bbarray.rb