| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Array | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # Equal to `self[1]`. | 
					
						
							| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ### Example | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # ```ruby | 
					
						
							|  |  |  |   # %w( a b c d e ).second # => "b" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  |   def second = self[1] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # Equal to `self[2]`. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # ### Example | 
					
						
							| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ```ruby | 
					
						
							|  |  |  |   # %w( a b c d e ).third # => "c" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  |   def third = self[2] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # Equal to `self[3]`. | 
					
						
							| 
									
										
										
										
											2024-01-11 14:37:09 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ### Example | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # ```ruby | 
					
						
							|  |  |  |   # %w( a b c d e ).fourth # => "d" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2024-01-11 14:37:09 -08:00
										 |  |  |   def fourth = self[3] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # Equal to `self[4]`. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # ### Example | 
					
						
							| 
									
										
										
										
											2024-01-11 19:22:16 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ```ruby | 
					
						
							|  |  |  |   # %w( a b c d e ).fifth # => "e" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2024-01-11 19:22:16 -08:00
										 |  |  |   def fifth = self[4] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |   # Converts the array to a comma-separated sentence where the last element is | 
					
						
							|  |  |  |   # joined by the connector word. | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ### Examples | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ```ruby | 
					
						
							|  |  |  |   # [].to_sentence                      # => "" | 
					
						
							|  |  |  |   # ['one'].to_sentence                 # => "one" | 
					
						
							|  |  |  |   # ['one', 'two'].to_sentence          # => "one and two" | 
					
						
							|  |  |  |   # ['one', 'two', 'three'].to_sentence # => "one, two and three" | 
					
						
							|  |  |  |   # ['one', 'two'].to_sentence(two_words_connector: '-') | 
					
						
							|  |  |  |   # # => "one-two" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # ``` | 
					
						
							|  |  |  |   # ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') | 
					
						
							|  |  |  |   # # => "one or two or at least three" | 
					
						
							|  |  |  |   # ``` | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @see https://github.com/rails/rails/blob/v7.0.4.2/activesupport/lib/active_support/core_ext/array/conversions.rb#L8-L84 | 
					
						
							|  |  |  |   #   ActiveSupport Array#to_sentence monkey-patch | 
					
						
							| 
									
										
										
										
											2023-03-03 12:43:15 -08:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2023-03-03 12:43:15 -08:00
										 |  |  |   # Copyright (c) David Heinemeier Hansson | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # Permission is hereby granted, free of charge, to any person obtaining | 
					
						
							|  |  |  |   # a copy of this software and associated documentation files (the | 
					
						
							|  |  |  |   # "Software"), to deal in the Software without restriction, including | 
					
						
							|  |  |  |   # without limitation the rights to use, copy, modify, merge, publish, | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # distribute, sublicense and/or sell copies of the Software and to | 
					
						
							| 
									
										
										
										
											2023-03-03 12:43:15 -08:00
										 |  |  |   # permit persons to whom the Software is furnished to do so, subject to | 
					
						
							|  |  |  |   # the following conditions: | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # The above copyright notice and this permission notice shall be | 
					
						
							|  |  |  |   # included in all copies or substantial portions of the Software. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
					
						
							|  |  |  |   # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
					
						
							|  |  |  |   # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
					
						
							|  |  |  |   # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | 
					
						
							|  |  |  |   # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | 
					
						
							|  |  |  |   # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | 
					
						
							|  |  |  |   # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |   # | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @param [String] words_connector The sign or word used to join all but the last | 
					
						
							|  |  |  |   #   element in arrays with three or more elements (default: `", "`). | 
					
						
							|  |  |  |   # @param [String] last_word_connector The sign or word used to join the last element | 
					
						
							|  |  |  |   #   in arrays with three or more elements (default: `" and "`). | 
					
						
							|  |  |  |   # @param [String] two_words_connector The sign or word used to join the elements | 
					
						
							|  |  |  |   #   in arrays with two elements (default: `" and "`). | 
					
						
							| 
									
										
										
										
											2024-01-10 12:07:04 -08:00
										 |  |  |   sig { params(words_connector: String, two_words_connector: String, last_word_connector: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2023-03-08 09:46:01 -08:00
										 |  |  |   def to_sentence(words_connector: ", ", two_words_connector: " and ", last_word_connector: " and ") | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |     case length | 
					
						
							|  |  |  |     when 0
 | 
					
						
							|  |  |  |       +"" | 
					
						
							|  |  |  |     when 1
 | 
					
						
							| 
									
										
										
										
											2023-03-02 14:39:55 -08:00
										 |  |  |       # This is not typesafe, if the array contains a BasicObject | 
					
						
							|  |  |  |       +T.unsafe(self[0]).to_s | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |     when 2
 | 
					
						
							| 
									
										
										
										
											2024-09-04 10:05:50 -07:00
										 |  |  |       "#{self[0]}#{two_words_connector}#{self[1]}" | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2024-09-04 10:05:50 -07:00
										 |  |  |       "#{T.must(self[0...-1]).join(words_connector)}#{last_word_connector}#{self[-1]}" | 
					
						
							| 
									
										
										
										
											2023-03-02 14:26:47 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |