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) ⇒ Object
Get the elements unique to one of two arrays.
- #bottom!(n = 1, &blk) ⇒ Object
- #bottom_by!(n = 1, &blk) ⇒ Object
-
#delete_all(&_block) ⇒ Object
Deletes every element of
self
for which block evaluates totrue
. - #extend(*args, &block) ⇒ Object
-
#extend!(size, *rest) ⇒ Object
Extend this Array.
-
#minus(ary, remainder: false) ⇒ Object
Subtract elements from this array.
-
#samples(min: nil, max: nil, random: nil) ⇒ Object
Choose a random subset of elements from the array.
-
#to_b ⇒ Object
Converts ary to a boolean.
-
#to_proc ⇒ Object
Returns a Proc that accepts a single argument.
- #top!(n = 1, &blk) ⇒ Object
- #top_by!(n = 1, &blk) ⇒ Object
Instance Method Details
#^(other) ⇒ Object
Get the elements unique to one of two arrays.
Duplicates in either array are included only once.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mug/array/minus.rb', line 42 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, &blk) ⇒ Object
104 105 106 |
# File 'lib/mug/top.rb', line 104 def bottom! n=1, &blk replace bottom(n, &blk) end |
#bottom_by!(n = 1, &blk) ⇒ Object
107 108 109 110 |
# File 'lib/mug/top.rb', line 107 def bottom_by! n=1, &blk return enum_for(:bottom_by!, n) unless block_given? replace bottom_by(n, &blk) end |
#delete_all(&_block) ⇒ Object
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.
See #delete_if, #reject!
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mug/array/delete_all.rb', line 13 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(*args, &block) ⇒ Object
48 49 50 |
# File 'lib/mug/array/extend.rb', line 48 def extend *args, &block dup.extend!(*args, &block) 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) ⇒ Object
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)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mug/array/minus.rb', line 16 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) ⇒ Object
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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mug/array/samples.rb', line 20 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 ⇒ Object
Converts ary to a boolean. Returns true if not empty.
66 67 68 |
# File 'lib/mug/bool.rb', line 66 def to_b !empty? end |
#to_proc ⇒ Object
Returns a Proc that accepts a single argument.
The Proc’s parameter is used as an index into this array.
9 10 11 |
# File 'lib/mug/array/to_proc.rb', line 9 def to_proc proc {|i| self[i] } end |
#top!(n = 1, &blk) ⇒ Object
97 98 99 |
# File 'lib/mug/top.rb', line 97 def top! n=1, &blk replace top(n, &blk) end |
#top_by!(n = 1, &blk) ⇒ Object
100 101 102 103 |
# File 'lib/mug/top.rb', line 100 def top_by! n=1, &blk return enum_for(:top_by!, n) unless block_given? replace top_by(n, &blk) end |