class Array

Public Instance Methods

^(other) click to toggle source

Get the elements unique to one of two arrays.

Duplicates in either array are included only once.

# 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) click to toggle source
# File lib/mug/top.rb, line 104
def bottom! n=1, &blk
  replace bottom(n, &blk)
end
bottom_by!(n=1, &blk) click to toggle source
# 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() { |e| ... } click to toggle source

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!

# 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) click to toggle source

@see extend!

# File lib/mug/array/extend.rb, line 48
def extend *args, &block
  dup.extend!(*args, &block)
end
extend!(size, *rest) { |i| ... } click to toggle source

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). @see concat @see +

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 }

# 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) click to toggle source

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)

# 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) click to toggle source

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.

# 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() click to toggle source

Converts ary to a boolean. Returns true if not empty.

# File lib/mug/bool.rb, line 66
def to_b
  !empty?
end
to_proc() click to toggle source

Returns a Proc that accepts a single argument.

The Proc's parameter is used as an index into this array.

# File lib/mug/array/to_proc.rb, line 9
def to_proc
  method(:slice).to_proc
end
top!(n=1, &blk) click to toggle source
# File lib/mug/top.rb, line 97
def top! n=1, &blk
  replace top(n, &blk)
end
top_by!(n=1, &blk) click to toggle source
# 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