aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2009-08-30 21:37:45 +0200
committerXavier Noria <fxn@hashref.com>2009-08-30 21:43:17 +0200
commit0ed583f59a026965d7480345275baf803d37cd48 (patch)
tree9018176a69119ce2ac6da8f61846c4f2cd47a73d /railties/guides
parent0af0a3db771440fe01b3e8df6e550a759ac87569 (diff)
downloadrails-0ed583f59a026965d7480345275baf803d37cd48.tar.gz
rails-0ed583f59a026965d7480345275baf803d37cd48.tar.bz2
rails-0ed583f59a026965d7480345275baf803d37cd48.zip
AS guide: explains Array#to_sentence
Diffstat (limited to 'railties/guides')
-rw-r--r--railties/guides/source/active_support_overview.textile28
1 files changed, 28 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile
index 8e4391cdd0..4f18f0c496 100644
--- a/railties/guides/source/active_support_overview.textile
+++ b/railties/guides/source/active_support_overview.textile
@@ -817,6 +817,34 @@ You can pick a random element with +rand+:
shape_type = [Circle, Square, Triangle].rand
</ruby>
+h4. Conversions
+
+h5. +to_sentence+
+
+The method +to_sentence+ turns an array into a string containing a sentence the enumerates its items:
+
+<ruby>
+%w().to_sentence # => ""
+%w(Earth).to_sentence # => "Earth"
+%w(Earth Wind).to_sentence # => "Earth and Wind"
+%w(Earth Wind Fire).to_sentence # => "Earth, Wind, and Fire"
+</ruby>
+
+This method accepts three options:
+
+* <tt>:two_words_connector</tt>: What is used for arrays of length 2. Default is " and ".
+* <tt>:words_connector</tt>: What is used to join the elements of arrays with 3 or more elements, except for the last two. Default is ", ".
+* <tt>:last_word_connector</tt>: What is used to join the last items of an array with 3 or more elements. Default is ", and ".
+
+The defaults for these options can be localised, their keys are:
+
+|_. Option |_. I18n key |
+| <tt>:two_words_connector</tt> | <tt>support.array.two_words_connector</tt> |
+| <tt>:words_connector</tt> | <tt>support.array.words_connector</tt> |
+| <tt>:last_word_connector</tt> | <tt>support.array.last_word_connector</tt> |
+
+Options <tt>:connector</tt> and <tt>:skip_last_comma</tt> are deprecated.
+
h4. Wrapping
The class method +Array.wrap+ behaves like the function +Array()+ except that it does not try to call +to_a+ on its argument. That changes the behaviour for enumerables: