joined 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/joined.gemspec +1 -1
- data/lib/joined.rb +7 -2
- data/test/test_joined.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 508cc1389e031ef74fd7e700066a4b7642d10db2754acc384670fce2277ea68d
|
4
|
+
data.tar.gz: c69ddae19a6ddaaeb349111a5cb076f3fb75466584d98953c0c93d47d6247564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a34b32456b37ec9fb5fcba35b64a28153b4eb76bf89e8afadec977df8f7ad9d1dbf9b4aee90b9df01d915734531355e5ed41ea5970a5cd9e70b51bd488216d06
|
7
|
+
data.tar.gz: c5b39a60e832cdee4b2a1e02a16b31501bbdf0718c5fd04fef4e1b9243054e713d197203051476dc6387c5d9492d492ed8e2c3279d3d697d9b46e087fb6f41c2
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
[](https://hitsofcode.com/view/github/yegor256/joined)
|
12
12
|
[](https://github.com/yegor256/joined/blob/master/LICENSE.txt)
|
13
13
|
|
14
|
-
|
14
|
+
Do you know [`to_sentence`][to_sentence] from Rails?
|
15
15
|
This gem does exactly the same, but without Rails.
|
16
16
|
|
17
17
|
```ruby
|
data/joined.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
10
10
|
s.required_ruby_version = '>=3.2'
|
11
11
|
s.name = 'joined'
|
12
|
-
s.version = '0.0
|
12
|
+
s.version = '0.1.0'
|
13
13
|
s.license = 'MIT'
|
14
14
|
s.summary = 'A simple Ruby gem that adds a .joined() method to Array'
|
15
15
|
s.description =
|
data/lib/joined.rb
CHANGED
@@ -9,10 +9,15 @@
|
|
9
9
|
# Copyright:: Copyright (c) 2025 Yegor Bugayenko
|
10
10
|
# License:: MIT
|
11
11
|
class Array
|
12
|
-
|
12
|
+
# Join strings into a single line, splitting them with comma
|
13
|
+
# and placing "AND" between the last two items.
|
14
|
+
#
|
15
|
+
# @param [Boolean] oxford Should we place a comma before AND?
|
16
|
+
# @return [String] The text generated
|
17
|
+
def joined(oxford: true)
|
13
18
|
return '' if empty?
|
14
19
|
return first if length == 1
|
15
20
|
|
16
|
-
"#{self[0...-1].join(', ')}#{',' if length > 2} and #{self[-1]}"
|
21
|
+
"#{self[0...-1].join(', ')}#{',' if length > 2 && oxford} and #{self[-1]}"
|
17
22
|
end
|
18
23
|
end
|
data/test/test_joined.rb
CHANGED
@@ -17,4 +17,12 @@ class Testjoined < Minitest::Test
|
|
17
17
|
assert_equal('apple, banana, and orange', %w[apple banana orange].joined)
|
18
18
|
assert_equal('apple, banana, orange, and pear', %w[apple banana orange pear].joined)
|
19
19
|
end
|
20
|
+
|
21
|
+
def test_without_oxford_comma
|
22
|
+
assert_equal('', [].joined(oxford: false))
|
23
|
+
assert_equal('apple', ['apple'].joined(oxford: false))
|
24
|
+
assert_equal('apple and banana', %w[apple banana].joined(oxford: false))
|
25
|
+
assert_equal('apple, banana and orange', %w[apple banana orange].joined(oxford: false))
|
26
|
+
assert_equal('apple, banana, orange and pear', %w[apple banana orange pear].joined(oxford: false))
|
27
|
+
end
|
20
28
|
end
|