1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module Array #:nodoc: 4 # Makes it easier to access parts of an array. 5 module Access 6 # Returns the tail of the array from +position+. 7 # 8 # %w( a b c d ).from(0) # => %w( a b c d ) 9 # %w( a b c d ).from(2) # => %w( c d ) 10 # %w( a b c d ).from(10) # => nil 11 # %w().from(0) # => nil 12 def from(position) 13 self[position..-1] 14 end 15 16 # Returns the beginning of the array up to +position+. 17 # 18 # %w( a b c d ).to(0) # => %w( a ) 19 # %w( a b c d ).to(2) # => %w( a b c ) 20 # %w( a b c d ).to(10) # => %w( a b c d ) 21 # %w().to(0) # => %w() 22 def to(position) 23 self[0..position] 24 end 25 26 # Equal to <tt>self[1]</tt>. 27 def second 28 self[1] 29 end 30 31 # Equal to <tt>self[2]</tt>. 32 def third 33 self[2] 34 end 35 36 # Equal to <tt>self[3]</tt>. 37 def fourth 38 self[3] 39 end 40 41 # Equal to <tt>self[4]</tt>. 42 def fifth 43 self[4] 44 end 45 46 # Equal to <tt>self[41]</tt>. Also known as accessing "the reddit". 47 def forty_two 48 self[41] 49 end 50 end 51 end 52 end 53 end