Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/joined.rb
Overview
Joins elements of array.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#joined(oxford: true, words_connector: ', ', last_word_connector: ', and ') ⇒ String
Join strings into a single line, splitting them with comma and placing “AND” between the last two items.
Instance Method Details
#joined(oxford: true, words_connector: ', ', last_word_connector: ', and ') ⇒ String
Join strings into a single line, splitting them with comma and placing “AND” between the last two items.
26 27 28 29 30 31 32 33 34 |
# File 'lib/joined.rb', line 26 def joined(oxford: true, words_connector: ', ', last_word_connector: ', and ') return '' if empty? return first if length == 1 final_connector = (last_word_connector || '').dup final_connector.sub!(/^,/, '') unless oxford && length > 2 "#{self[0...-1].join(words_connector)}#{final_connector}#{self[-1]}" end |