Today I Learned

hashrocket A Hashrocket project

You can turn arrays into sentences!

There's a neat little method Array#to_sentence that turns your array into a string in sentence format.

It's pretty self-explanatory, so here are some examples:

superheroes = ["Spider-Man", "Batman", "Iron man", "Wonder woman"]

superheroes.to_sentence
=> "Spider-Man, Batman, Iron man, and Wonder woman"

superheroes.to_sentence(
  words_connector: " or ", 
  last_word_connector: " or maybe even "
)
=> "Spider-Man or Batman or Iron man or maybe even Wonder woman"

With slight configuration, you can even have this work in different locales. Check it out in the docs!

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.