Project

joined

0.0
No release in over 3 years
This gem adds a .joined() method to the Array class. This method concatenates all elements of the array into a string, separating them with commas and placing "and" before the last element.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

Adds a .joined method to Array

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

Do you know to_sentence from Rails? This gem does exactly the same, but without Rails.

require 'joined'
puts ['orange', 'banana', 'pear'].joined

Prints:

orange, banana, and pear

You can also limit the number of displayed elements:

require 'joined'
puts ['apple', 'orange', 'banana', 'pear', 'grape'].joined(max: 3)

Prints:

apple, orange, banana, etc.

That's it.

Options

The joined method supports the following parameters:

  • words_connector (String) (defaults to: ", ") - the string used to join all but the last element of the list.
  • last_word_connector (String) (defaults to: ", and ") - the string used to join the last element of the list.
  • oxford (Boolean) (defaults to: true) - should we place a comma before the last_word_connector? If false, it will remove a leading comma from the last_word_connector. If true, it will preserve the leading comma specified in the last_word_connector, but it will not insert one if not already present.
  • comma_before (Boolean) (defaults to: false) - If true, adds the comma inside quotation marks (e.g. "one," "two," and "three"). If false, adds the comma outside quotation marks (e.g. "one", "two", and "three").
  • max (Integer) (defaults to: nil) - Maximum number of elements to show. If the array has more elements than this value, only the first max elements will be displayed, followed by ", etc.".

See the Yard docs for full gem documentation.

How to contribute

Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then:

bundle update
bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.