Sha256: 57ac46a74d4f260e20abcc5ac4d6e051f58d6c252e8fe4cf0d09147ad165fe10

Contents?: true

Size: 1.35 KB

Versions: 53

Compression:

Stored size: 1.35 KB

Contents

class Array
  # Returns the tail of the array from +position+.
  #
  #   %w( a b c d ).from(0)  # => ["a", "b", "c", "d"]
  #   %w( a b c d ).from(2)  # => ["c", "d"]
  #   %w( a b c d ).from(10) # => []
  #   %w().from(0)           # => []
  #   %w( a b c d ).from(-2) # => ["c", "d"]
  #   %w( a b c ).from(-10)  # => []
  def from(position)
    self[position, length] || []
  end

  # Returns the beginning of the array up to +position+.
  #
  #   %w( a b c d ).to(0)  # => ["a"]
  #   %w( a b c d ).to(2)  # => ["a", "b", "c"]
  #   %w( a b c d ).to(10) # => ["a", "b", "c", "d"]
  #   %w().to(0)           # => []
  #   %w( a b c d ).to(-2) # => ["a", "b", "c"]
  #   %w( a b c ).to(-10)  # => []
  def to(position)
    if position >= 0
      first position + 1
    else
      self[0..position]
    end
  end

  # Equal to <tt>self[1]</tt>.
  #
  #   %w( a b c d e ).second # => "b"
  def second
    self[1]
  end

  # Equal to <tt>self[2]</tt>.
  #
  #   %w( a b c d e ).third # => "c"
  def third
    self[2]
  end

  # Equal to <tt>self[3]</tt>.
  #
  #   %w( a b c d e ).fourth # => "d"
  def fourth
    self[3]
  end

  # Equal to <tt>self[4]</tt>.
  #
  #   %w( a b c d e ).fifth # => "e"
  def fifth
    self[4]
  end

  # Equal to <tt>self[41]</tt>. Also known as accessing "the reddit".
  #
  #   (1..42).to_a.forty_two # => 42
  def forty_two
    self[41]
  end
end

Version data entries

53 entries across 51 versions & 10 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/core_ext/array/access.rb
activesupport-4.2.11.2 lib/active_support/core_ext/array/access.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/array/access.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/array/access.rb
activesupport-4.2.11.1 lib/active_support/core_ext/array/access.rb
activesupport-4.2.11 lib/active_support/core_ext/array/access.rb
activesupport-4.2.10 lib/active_support/core_ext/array/access.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/array/access.rb
activesupport-4.2.9 lib/active_support/core_ext/array/access.rb
activesupport-4.2.9.rc2 lib/active_support/core_ext/array/access.rb
activesupport-4.2.9.rc1 lib/active_support/core_ext/array/access.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/core_ext/array/access.rb
activesupport-4.2.8 lib/active_support/core_ext/array/access.rb
activesupport-4.2.8.rc1 lib/active_support/core_ext/array/access.rb
activesupport-4.2.7.1 lib/active_support/core_ext/array/access.rb
activesupport-4.2.7 lib/active_support/core_ext/array/access.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/array/access.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/array/access.rb
activesupport-4.2.6 lib/active_support/core_ext/array/access.rb
activesupport-4.2.6.rc1 lib/active_support/core_ext/array/access.rb