Class: Array
Instance Method Summary collapse
- #after(value) ⇒ Object
- #before(value) ⇒ Object
- #delete_first ⇒ Object
- #delete_first! ⇒ Object
- #delete_last ⇒ Object
- #delete_last! ⇒ Object
- #delete_values(*args) ⇒ Object
- #duplicates(minimum = 2) ⇒ Object
- #from(position) ⇒ Object
- #groups(number) ⇒ Object
- #in_groups(number, fill_with = nil) ⇒ Object
- #in_groups_of(number, fill_with = nil) ⇒ Object
- #probability ⇒ Object
- #reject_values(*args) ⇒ Object
- #sample! ⇒ Object
- #split(number = nil) ⇒ Object
- #strip ⇒ Object
- #strip! ⇒ Object
- #to(position) ⇒ Object
- #to_sentence(options = {}) ⇒ Object
Instance Method Details
#after(value) ⇒ Object
3 4 5 6 7 |
# File 'lib/active_object/array.rb', line 3 def after(value) return(nil) unless include?(value) self[(index(value).to_i + 1) % size] end |
#before(value) ⇒ Object
9 10 11 12 13 |
# File 'lib/active_object/array.rb', line 9 def before(value) return(nil) unless include?(value) self[(index(value).to_i - 1) % size] end |
#delete_first ⇒ Object
15 16 17 |
# File 'lib/active_object/array.rb', line 15 def delete_first self[1..-1] end |
#delete_first! ⇒ Object
19 20 21 |
# File 'lib/active_object/array.rb', line 19 def delete_first! replace(delete_first) end |
#delete_last ⇒ Object
23 24 25 |
# File 'lib/active_object/array.rb', line 23 def delete_last self[0...-1] end |
#delete_last! ⇒ Object
27 28 29 |
# File 'lib/active_object/array.rb', line 27 def delete_last! replace(delete_last) end |
#delete_values(*args) ⇒ Object
31 32 33 34 35 |
# File 'lib/active_object/array.rb', line 31 def delete_values(*args) result = [] args.each { |v| result << delete(v) } return(result) end |
#duplicates(minimum = 2) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/active_object/array.rb', line 37 def duplicates(minimum=2) hash = Hash.new(0) each { |i| hash[i] += 1 } hash.delete_if { |k, v| v < minimum }.keys end |
#from(position) ⇒ Object
45 46 47 |
# File 'lib/active_object/array.rb', line 45 def from(position) self[position, size] || [] end |
#groups(number) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/active_object/array.rb', line 50 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_object/array.rb', line 58 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_object/array.rb', line 77 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 |
#probability ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_object/array.rb', line 94 def probability hash = Hash.new(0.0) differ = 0.0 each do |e| hash[e] += 1.0 differ += 1.0 end hash.keys.each { |e| hash[e] /= differ } hash end |
#reject_values(*args) ⇒ Object
107 108 109 |
# File 'lib/active_object/array.rb', line 107 def reject_values(*args) reject { |x| args.include?(x) } end |
#sample! ⇒ Object
111 112 113 |
# File 'lib/active_object/array.rb', line 111 def sample! delete_at(Random.rand(size - 1)) end |
#split(number = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_object/array.rb', line 116 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
138 139 140 |
# File 'lib/active_object/array.rb', line 138 def strip reject { |v| v.blank? } end |
#strip! ⇒ Object
142 143 144 |
# File 'lib/active_object/array.rb', line 142 def strip! replace(strip) end |
#to(position) ⇒ Object
147 148 149 |
# File 'lib/active_object/array.rb', line 147 def to(position) position >= 0 ? first(position + 1) : self[0..position] end |
#to_sentence(options = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/active_object/array.rb', line 153 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 |