Class: Array
Instance Method Summary collapse
-
#all ⇒ Object
for easier Sequel query.
- #blank? ⇒ Boolean
-
#last=(what) ⇒ Object
set last element of an array.
-
#random_by_string(string) ⇒ Object
will return fixed element for any random string.
-
#to_csv ⇒ Object
convert list of lists to CSV.
-
#to_sentence(opts = {}) ⇒ Object
convert list to sentence, Rails like.
-
#toggle(element) ⇒ Object
toggle existance of an element in array and return true when one added.
-
#wrap(name, opts = {}) ⇒ Object
wrap all list elements with a tag.
Instance Method Details
#all ⇒ Object
for easier Sequel query
57 58 59 |
# File 'lib/overload/array.rb', line 57 def all self end |
#blank? ⇒ Boolean
37 38 39 |
# File 'lib/overload/blank.rb', line 37 def blank? self.length == 0 end |
#last=(what) ⇒ Object
set last element of an array
21 22 23 |
# File 'lib/overload/array.rb', line 21 def last= what self[self.length-1] = what end |
#random_by_string(string) ⇒ Object
will return fixed element for any random string
62 63 64 65 |
# File 'lib/overload/array.rb', line 62 def random_by_string string i = string.split('').map{ |_| _.ord }.sum self[i % length] end |
#to_csv ⇒ Object
convert list of lists to CSV
3 4 5 6 7 8 9 10 11 12 13 |
# 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 = {}) ⇒ Object
convert list to sentence, Rails like
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
toggle existance of an element in array and return true when one added
43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 = {}) ⇒ Object
wrap all list elements with a tag
16 17 18 |
# File 'lib/overload/array.rb', line 16 def wrap name, opts={} map{ |el| opts.tag(name, opts) } end |