Permalink
Browse files

AS guide: explains extensions to Array for accessing elements

  • Loading branch information...
1 parent a66909c commit dc7323efd34327c13d26031b68e51314c24360f6 @fxn fxn committed May 31, 2009
Showing with 24 additions and 1 deletion.
  1. +24 −1 railties/guides/source/active_support_overview.textile
@@ -220,7 +220,30 @@ h3. Extensions to +Enumerable+
h3. Extensions to +Array+
-...
+h4. Accessing
+
+Active Support augments the API of arrays to ease certain ways of accessing them or subsets of them. For example, +to+ returns the subarray of elements up to the one at the passed index:
+
+<ruby>
+%w(a b c d).to(2) # => %w(a b c)
+[].to(7) # => []
+</ruby>
+
+Similarly, +from+ returns the tail from the element at the passed index on:
+
+<ruby>
+%w(a b c d).from(2) # => %w(c d)
+%w(a b c d).from(10) # => nil
+[].from(0) # => nil
+</ruby>
+
+The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is builtin). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available.
+
+You can pick a random element with +rand+:
+
+<ruby>
+shape_type = [Circle, Square, Triangle].rand
+</ruby>
h3. Extensions to +Hash+

0 comments on commit dc7323e

Please sign in to comment.