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)
self[0..position]
end
# Equal to self[1].
#
# %w( a b c d e ).second # => "b"
def second
self[1]
end
# Equal to self[2].
#
# %w( a b c d e ).third # => "c"
def third
self[2]
end
# Equal to self[3].
#
# %w( a b c d e ).fourth # => "d"
def fourth
self[3]
end
# Equal to self[4].
#
# %w( a b c d e ).fifth # => "e"
def fifth
self[4]
end
# Equal to self[41]. Also known as accessing "the reddit".
#
# (1..42).to_a.forty_two # => 42
def forty_two
self[41]
end
end