Sha256: 394d7f1e9462419b8443fb2f207c2aec55abf5d3cf483621015daba877fb74d2

Contents?: true

Size: 646 Bytes

Versions: 8

Compression:

Stored size: 646 Bytes

Contents

class Array

  # Returns last _n_ elements.
  #
  #   %w{W o r l d}.from(3)  #=> %w{l d}
  #
  def from(i)
    return self if i >= size
    self[i, size - i]
  end unless method_defined?(:from)

  # Fetch values from a start index thru an end index.
  #
  #   [1,2,3,4,5].thru(0,2)  #=> [1,2,3]
  #   [1,2,3,4,5].thru(2,4)  #=> [3,4,5]
  #
  #   [1,2,3,4,5].thru(2)  #=> [1,2,3]
  #   [1,2,3,4,5].thru(4)  #=> [1,2,3,4,5]
  #
  def thru(from, to=nil)
    from, to = 0, from unless to
    to = size - 1 if to >= size
    a = []
    i = from
    while i <= to
      a << self[i]
      i += 1
    end
    a
  end unless method_defined?(:thru)

end

Version data entries

8 entries across 7 versions & 1 rubygems

Version Path
facets-3.0.0 lib/core/facets/array/from.rb
facets-2.9.3 lib/core/facets/array/from.rb
facets-2.9.2 lib/core/facets/array/from.rb
facets-2.9.2 src/core/facets/array/from.rb
facets-2.9.1 lib/core/facets/array/from.rb
facets-2.9.0 lib/core/facets/array/from.rb
facets-2.9.0.pre.2 lib/core/facets/array/from.rb
facets-2.9.0.pre.1 lib/core/facets/array/from.rb