lib/linked/listable.rb
def take(n)
      raise ArgumentError, 'n must be an integer' unless n.is_a? Integer

      # Optimize for the most simple cases
      return [self] if n == 1 || n == -1
      return [] if n.zero?

      n_abs = n.abs

      res = Array.new n_abs

      if n.positive?
        res[0] = self
        enum = after
        iter = 1.upto(n_abs - 1)
      else
        res[n_abs - 1] = self
        enum = before
        iter = (n_abs - 2).downto 0
      end

      iter.each { |i| res[i] = enum.next }
      res
    rescue StopIteration
      res.compact!
      res
    end
def take(n)
      raise ArgumentError, 'n must be an integer' unless n.is_a? Integer

      # Optimize for the most simple cases
      return [self] if n == 1 || n == -1
      return [] if n.zero?

      n_abs = n.abs

      res = Array.new n_abs

      if n.positive?
        res[0] = self
        enum = after
        iter = 1.upto(n_abs - 1)
      else
        res[n_abs - 1] = self
        enum = before
        iter = (n_abs - 2).downto 0
      end

      iter.each { |i| res[i] = enum.next }
      res
    rescue StopIteration
      res.compact!
      res
    end
rubocop:disable Metrics/AbcSize
 rubocop:disable Metrics/CyclomaticComplexity
 rubocop:disable Metrics/MethodLength
 rubocop:disable Metrics/PerceivedComplexity
 Take n items and put them into a sorted array. If n is positive the array
 will include this item, as well the n - 1 items following it in the chain.
 If n is negative the items will be taken from before this item instead.

 If there are less than n - 1 items before/after this the resulting array
 will contain less than n items.

 @raise  [ArgumentError] if `n` is not an integer.

 @param  n [Integer] the number of items to take.
 @return [Array<Listable>] an array containing the taken items.