Class: Array
- Includes:
- LuxJson
- Defined in:
- lib/overload/json.rb,
lib/overload/array.rb,
lib/overload/blank.rb
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 ‘@list.random_by_string(’foo’)‘.
- #shift_push ⇒ Object
-
#to_csv ⇒ Object
Aonvert list of lists to CSV.
-
#to_sentence(opts = {}) ⇒ Object
Convert list to sentence, Rails like ‘@list.to_sentence(words_connector: ’, ‘, two_words_connector: ’ and ‘, last_word_connector: ’, and ‘)`.
-
#to_ul(klass = nil) ⇒ Object
Convert list to HTML UL list ‘@list.to_ul(:foo) # <ul class=“foo”><li>…`.
-
#toggle(element) ⇒ Object
Toggle existance of an element in array and return true when one added ‘@list.toggle(:foo)`.
-
#wrap(name, opts = {}) ⇒ Object
Wrap all list elements with a tag.
- #xmap ⇒ Object
- #xuniq ⇒ Object
Methods included from LuxJson
#to_jsonc, #to_jsonp, #to_jsons
Instance Method Details
#all ⇒ Object
for easier Sequel query
59 60 61 |
# File 'lib/overload/array.rb', line 59 def all self 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 ‘@list.random_by_string(’foo’)‘
65 66 67 68 |
# File 'lib/overload/array.rb', line 65 def random_by_string string i = string.split('').map{ |_| _.ord }.sum self[i % length] end |
#shift_push ⇒ Object
80 81 82 83 84 |
# File 'lib/overload/array.rb', line 80 def shift_push next_item = shift push next_item next_item end |
#to_csv ⇒ Object
Aonvert 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 ‘@list.to_sentence(words_connector: ’, ‘, two_words_connector: ’ and ‘, last_word_connector: ’, and ‘)`
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/overload/array.rb', line 27 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].to_s + last_word.to_s end |
#to_ul(klass = nil) ⇒ Object
Convert list to HTML UL list ‘@list.to_ul(:foo) # <ul class=“foo”><li>…`
76 77 78 |
# File 'lib/overload/array.rb', line 76 def to_ul klass=nil %[<ul class="#{klass}">#{map{|el| "<li>#{el}</li>" }.join('')}</ul>] end |
#toggle(element) ⇒ Object
Toggle existance of an element in array and return true when one added ‘@list.toggle(:foo)`
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/overload/array.rb', line 45 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 |
#xmap ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/overload/array.rb', line 86 def xmap count = 0 map do |el| yield el, ++count el end end |
#xuniq ⇒ Object
70 71 72 |
# File 'lib/overload/array.rb', line 70 def xuniq uniq.select { |it| it.present? } end |