Adds a .joined
method to Array
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 thelast_word_connector
? If false, it will remove a leading comma from thelast_word_connector
. If true, it will preserve the leading comma specified in thelast_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 firstmax
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.