diff options
author | Xavier Noria <fxn@hashref.com> | 2009-06-01 00:19:05 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2009-06-01 00:19:05 +0200 |
commit | dc7323efd34327c13d26031b68e51314c24360f6 (patch) | |
tree | bf65c6bfb6434afa7ff941fd49e0202940989009 /railties/guides/source | |
parent | a66909c5c42503257271643ea01a13d2a9972262 (diff) | |
download | rails-dc7323efd34327c13d26031b68e51314c24360f6.tar.gz rails-dc7323efd34327c13d26031b68e51314c24360f6.tar.bz2 rails-dc7323efd34327c13d26031b68e51314c24360f6.zip |
AS guide: explains extensions to Array for accessing elements
Diffstat (limited to 'railties/guides/source')
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 7b3e03e09b..ef206422ed 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/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+ |