Sha256: ae2ded996f7fa3e4ec71ac8fc77c7889ac744e3f55ec0ef0e1ff0fe64d057d11

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

class Array

  def to_csv
    ret = []
    for row in self
    	add = []
    	for el in row
    		add << '"'+el.to_s.gsub(/\s+/,' ').gsub(/"/,"''")+'"'
	    end
	    ret.push(add.join(';'))
    end
    ret.join("\n")
  end

  def wrap(name, opts={})
    map{ |el| opts.tag(name, opts) }
  end

  def last=(what)
    self[self.length-1] = what
  end

  def to_sentence(opts={})
    opts[:words_connector]     ||= ', '
    opts[:two_words_connector] ||= ' and '
    opts[:last_word_connector] ||= ', and '

    len = self.length

    return '' if len == 0
    return self[0] if len == 1
    return self.join(opts[:two_words_connector]) if len == 2

    last_word = self.pop

    self.join(opts[:words_connector]) + opts[:last_word_connector] + last_word
  end

  # toggle element in array and return true when added
  def toggle(element)
    self.uniq!
    self.compact!
    if self.include?(element)
      self.delete(element)
      false
    else
      self.push(element)
      true
    end
  end

  def all
    self
  end

  # will return fixed element for any random string
  def random_by_string string
    i = string.split('').map{ |_| _.ord }.sum
    self[i % length]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.2.3 ./lib/overload/array.rb
lux-fw-0.2.1 ./lib/overload/array.rb