Class: Array
- Defined in:
- lib/mug/top.rb,
lib/mug/bool.rb,
lib/mug/array/minus.rb,
lib/mug/array/extend.rb,
lib/mug/array/samples.rb,
lib/mug/array/to_proc.rb,
lib/mug/array/delete_all.rb
Instance Method Summary collapse
-
#^(other) ⇒ Array
Get the elements unique to one of two arrays.
-
#bottom!(n = 1) {|a, b| ... } ⇒ Array
Replaces this array with the bottom
nitems in place. -
#bottom_by!(n = 1) {|item| ... } ⇒ Array, Enumerator
Replaces this array with the bottom
nitems (by mapping) in place. -
#delete_all {|item| ... } ⇒ Array, Enumerator
Deletes every element of
selffor which block evaluates totrue. - #extend ⇒ Object
-
#extend!(size, *rest) ⇒ Object
Extend this Array.
-
#minus(ary, remainder: false) ⇒ Array
Subtract elements from this array.
-
#samples(min: nil, max: nil, random: nil) ⇒ Array
Choose a random subset of elements from the array.
-
#to_b ⇒ Boolean
Converts ary to a boolean.
-
#to_proc ⇒ Proc
Returns a Proc that accepts one or two arguments.
-
#top!(n = 1) {|a, b| ... } ⇒ Array
Replaces this array with the top
nitems in place. -
#top_by!(n = 1) {|item| ... } ⇒ Array, Enumerator
Replaces this array with the top
nitems (by mapping) in place.
Instance Method Details
#^(other) ⇒ Array
Get the elements unique to one of two arrays.
Duplicates in either array are included only once.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mug/array/minus.rb', line 53 def ^ other left = uniq right = [] other.uniq.each do |x| if left.include? x left.delete x elsif ! right.include?(x) right << x end end left + right end |
#bottom!(n = 1) {|a, b| ... } ⇒ Array
Replaces this array with the bottom n items in place.
159 160 161 |
# File 'lib/mug/top.rb', line 159 def bottom! n=1, &blk replace bottom(n, &blk) end |
#bottom_by!(n = 1) {|item| ... } ⇒ Array, Enumerator
Replaces this array with the bottom n items (by mapping) in place.
170 171 172 173 |
# File 'lib/mug/top.rb', line 170 def bottom_by! n=1, &blk return enum_for(:bottom_by!, n) unless block_given? replace bottom_by(n, &blk) end |
#delete_all {|item| ... } ⇒ Array, Enumerator
Deletes every element of self for which block evaluates to true.
Returns an array of the deleted elements.
If no block is given, an Enumerator is returned instead.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mug/array/delete_all.rb', line 18 def delete_all &_block return enum_for :delete_all unless block_given? [].tap do |removed| delete_if do |e| if yield e removed << e true end end end end |
#extend ⇒ Object
48 49 50 |
# File 'lib/mug/array/extend.rb', line 48 def extend(...) dup.extend!(...) end |
#extend!(size, *rest) ⇒ Object
Extend this Array.
In the first form, when a size and an optional obj are sent,
the array is extended with size copies of obj. Take notice that
all elements will reference the same object obj.
The second form appends a copy of the array passed as a parameter (the array is generated by calling #to_ary on the parameter). In the last form, the array is extended by the given size. Each new element in the array is created by passing the element's index to the given block and storing the return value.
@call-seq extend!(size=0, obj=nil) @call-seq extend!(array) @call-seq extend!(size) {|index| block }
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mug/array/extend.rb', line 24 def extend! size, *rest raise ArgumentError, "wrong number of arguments (#{rest.length+1} for 1..2)" if rest.length > 1 # Same logic as array.c/rb_ary_initialize if rest.empty? && !size.is_a?(Integer) warn 'warning: given block not used' if block_given? concat size.to_ary return self end raise ArgumentError, 'negative size' if size < 0 a = length b = a+size if block_given? warn 'warning: block supersedes default value argument' if !rest.empty? fill(a...b) {|i| yield i } else obj = rest[0] fill(a...b) { obj } end end |
#minus(ary, remainder: false) ⇒ Array
For the single-argument case, Array#minus is similar to
Array#difference (Ruby 2.6+). The remainder: option and
Array#^ have no stdlib equivalent.
Subtract elements from this array.
This is similar to Array#- except that elements from this array are
removed only once per instance in ary.
If remainder is given and true, returns a second array which is
all elements in ary that were not present in this array.
@call-seq minus(ary) @call-seq minus(ary, remainder: true)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mug/array/minus.rb', line 24 def minus ary, remainder: false result = dup rem = [] ary.each do |x| i = result.index x if i result.delete_at i elsif remainder rem << x end end if remainder [result, rem] else result end end |
#samples(min: nil, max: nil, random: nil) ⇒ Array
Choose a random subset of elements from the array.
The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn't repeat itself unless the array already contained duplicate elements.
If the array is empty, always returns an empty array.
The optional min and max arguments restrict the size of the
returned array. min must be >= 0, and max must be >= min.
(Both values are clamped to the size of the array.)
The optional random argument will be used as the random number
generator.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mug/array/samples.rb', line 25 def samples min: nil, max: nil, random: nil min = 1 if min.nil? min = length if min > length max = length if max.nil? max = length if max > length raise ArgumentError, "min must be >= 0 (#{min})" if min < 0 raise ArgumentError, "min (#{min}) must be <= max (#{max})" if min > max if random n = random.rand(min..max) sample n, random: random else n = rand(min..max) sample n end end |
#to_b ⇒ Boolean
Converts ary to a boolean. Returns true if not empty.
112 113 114 |
# File 'lib/mug/bool.rb', line 112 def to_b !empty? end |
#to_proc ⇒ Proc
Returns a Proc that accepts one or two arguments.
The Proc's parameter is used as an index into this array.
18 19 20 |
# File 'lib/mug/array/to_proc.rb', line 18 def to_proc method(:slice).to_proc end |
#top!(n = 1) {|a, b| ... } ⇒ Array
Replaces this array with the top n items in place.
137 138 139 |
# File 'lib/mug/top.rb', line 137 def top! n=1, &blk replace top(n, &blk) end |
#top_by!(n = 1) {|item| ... } ⇒ Array, Enumerator
Replaces this array with the top n items (by mapping) in place.
148 149 150 151 |
# File 'lib/mug/top.rb', line 148 def top_by! n=1, &blk return enum_for(:top_by!, n) unless block_given? replace top_by(n, &blk) end |