Tip Details Add a Related Tip

Rate as: Positive Negative

Converting arrays to sentences in Ruby

I often find myself needing to convert an array of elements into a sentence in the view in my Ruby on Rails applications. For example, turning the array ['dog', 'cat', 'mouse'] into the text "dog, cat, and mouse".

Turns out, there is an array method called to_sentence that will perform this conversion automatically: arrayName.to_sentence. The to_sentence method allows you to control the connecting word ("and" in this case), and if there should be a comma placed before the connecting word. If your writing applications in Ruby on Rails, this method is a nice trick to know.
Link: api.rubyonrails.org...Search for more tips related to this link
Rating: no ratings, 0 total Votes
Categories: Ruby on Rails
Added: on Jun 28, 2007 at 6:08 am
Added By: an anonymous user

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Something to consider before switching platforms

100% positive, 0 comments
– Tip added by an anonymous user on Sep 25, 2007 at 7:31 pm

Testing regular expressions

100% positive, 0 comments
– Tip added by an anonymous user on Jan 25, 2008 at 2:10 pm

Heroku - Instant, live Rails Applications

100% positive, 0 comments
– Tip added by an anonymous user on Jan 11, 2008 at 3:11 am

Summary of Ruby on Rails resources

100% positive, 0 comments
– Tip added by an anonymous user on May 03, 2007 at 4:05 pm

Ruby on Rails tutorials

no ratings, 0 comments
– Tip added by an anonymous user on Jun 08, 2007 at 7:55 pm