Skip to content

yegor256/joined

Repository files navigation

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.".
  • etc (String) (defaults to: , etc.) - The suffix to append when max is specified and array is truncated.

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.

About

Ruby gem to join strings together with an AND before the last one

Topics

Resources

License

Stars

Watchers

Forks

Contributors 5

Languages