Class: Array
Instance Method Summary collapse
- #delete_first ⇒ Object
- #delete_last ⇒ Object
- #from(position) ⇒ Object
- #groups(number) ⇒ Object
- #in_groups(number, fill_with = nil) ⇒ Object
- #in_groups_of(number, fill_with = nil) ⇒ Object
- #split(number = nil) ⇒ Object
- #strip ⇒ Object
- #to(position) ⇒ Object
- #to_sentence(options = {}) ⇒ Object
Instance Method Details
#delete_first ⇒ Object
3 4 5 |
# File 'lib/active_object/array.rb', line 3 def delete_first self[1..-1] end |
#delete_last ⇒ Object
7 8 9 |
# File 'lib/active_object/array.rb', line 7 def delete_last self[0...-1] end |
#from(position) ⇒ Object
12 13 14 |
# File 'lib/active_object/array.rb', line 12 def from(position) self[position, size] || [] end |
#groups(number) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/active_object/array.rb', line 17 def groups(number) return([]) if number <= 0 n, r = size.divmod(number) collection = (0..(n - 1)).collect { |i| self[(i * number), number] } r > 0 ? collection << self[-r, r] : collection end |
#in_groups(number, fill_with = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_object/array.rb', line 25 def in_groups(number, fill_with=nil) collection_size = size division = collection_size.div(number) modulo = collection_size % number collection = [] start = 0 number.times do |i| grouping = division + (modulo > 0 && modulo > i ? 1 : 0) collection << last_group = slice(start, grouping) last_group << fill_with if fill_with != false && modulo > 0 && grouping == division start += grouping end block_given? ? collection.each { |g| yield(g) } : collection end |
#in_groups_of(number, fill_with = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_object/array.rb', line 44 def in_groups_of(number, fill_with=nil) if number.to_i <= 0 raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}" end if fill_with == false collection = self else padding = (number - size % number) % number collection = dup.concat(Array.new(padding, fill_with)) end block_given? ? collection.each_slice(number) { |slice| yield(slice) } : collection.each_slice(number).to_a end |
#split(number = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_object/array.rb', line 62 def split(number=nil) if block_given? inject([[]]) do |results, element| yield(element) ? results << [] : results.last << element results end else results, arr = [[]], dup until arr.empty? if (idx = arr.index(number)) results.last.concat(arr.shift(idx)) arr.shift results << [] else results.last.concat(arr.shift(arr.size)) end end results end end |
#strip ⇒ Object
84 85 86 |
# File 'lib/active_object/array.rb', line 84 def strip reject { |v| v.blank? } end |
#to(position) ⇒ Object
89 90 91 |
# File 'lib/active_object/array.rb', line 89 def to(position) position >= 0 ? first(position + 1) : self[0..position] end |
#to_sentence(options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_object/array.rb', line 95 def to_sentence(={}) .assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector) default_connectors = { words_connector: ', ', two_words_connector: ' and ', last_word_connector: ', and ' } = default_connectors.merge!() case size when 0 '' when 1 self[0].to_s.dup when 2 "#{self[0]}#{options[:two_words_connector]}#{self[1]}" else "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}" end end |