Sha256: dcb8e49a60179be69b29305faa606807ab8a5a849d78a37a089fb30418515779

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

class Array
  # convert list of lists to CSV
  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

  # wrap all list elements with a tag
  def wrap name, opts={}
    map{ |el| opts.tag(name, opts) }
  end

  # set last element of an array
  def last= what
    self[self.length-1] = what
  end

  # convert list to sentence, Rails like
  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 existance of an element in array and return true when one added
  def toggle element
    self.uniq!
    self.compact!

    if self.include?(element)
      self.delete(element)
      false
    else
      self.push(element)
      true
    end
  end

  # for easier Sequel query
  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

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/overload/array.rb
lux-fw-0.5.36 ./lib/overload/array.rb
lux-fw-0.5.35 ./lib/overload/array.rb
lux-fw-0.5.34 ./lib/overload/array.rb
lux-fw-0.5.33 ./lib/overload/array.rb
lux-fw-0.5.32 ./lib/overload/array.rb