class Array

Public Instance Methods

all() click to toggle source

for easier Sequel query

# File lib/overload/array.rb, line 57
def all
  self
end
blank?() click to toggle source
# File lib/overload/blank.rb, line 37
def blank?
  self.length == 0
end
last=(what) click to toggle source

set last element of an array

# File lib/overload/array.rb, line 21
def last= what
  self[self.length-1] = what
end
random_by_string(string) click to toggle source

will return fixed element for any random string

# File lib/overload/array.rb, line 62
def random_by_string string
  i = string.split('').map{ |_| _.ord }.sum
  self[i % length]
end
to_csv() click to toggle source

convert list of lists to CSV

# File lib/overload/array.rb, line 3
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
to_sentence(opts={}) click to toggle source

convert list to sentence, Rails like

# File lib/overload/array.rb, line 26
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) click to toggle source

toggle existance of an element in array and return true when one added

# File lib/overload/array.rb, line 43
def toggle element
  self.uniq!
  self.compact!

  if self.include?(element)
    self.delete(element)
    false
  else
    self.push(element)
    true
  end
end
wrap(name, opts={}) click to toggle source

wrap all list elements with a tag

# File lib/overload/array.rb, line 16
def wrap name, opts={}
  map{ |el| opts.tag(name, opts) }
end