Sha256: ec83e51ee4615e47146ec13cc5f2e3d885ae7d772d57c51531dd84f42a9234d5

Contents?: true

Size: 793 Bytes

Versions: 2

Compression:

Stored size: 793 Bytes

Contents

class Array
  def sorted_by?
    each_cons(2).all? { |a, b| ((yield a) <=> (yield b)) <= 0 }
  end

  def limit(offset)
    offset > 0 ? self.last(offset) : self.first(offset.abs)
  end

  def round_shift(element, step)
    new_element = [self.min, (element - step), self.max].median
    actual_step = element - new_element

    [new_element, actual_step]
  end

  def median
    return nil if self.empty?

    sorted = self.sort
    len = self.length

    (sorted[(len - 1) / 2] + sorted[len / 2]) / 2
  end

  def find_by(key, val)
    self.where(key, val).first
  end

  def where(key, val = nil, &block)
    self.select do |element|
      key_eval = element.send(key)

      block_given? ? block.call(key_eval) : (key_eval == val)
    end
  end

  def ensure_array
    self.to_a
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nin-1.3.0 lib/nin/extensions/array_extensions.rb
nin-1.2.0 lib/nin/extensions/array_extensions.rb