Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite088b8e

Browse files
committed
Add ability to join nouns into a sentence
1 parent00816d4 commite088b8e

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

‎lib/strings/inflection.rb‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
2-
2+
require_relative"inflection/combined_noun"
33
require_relative"inflection/configuration"
44
require_relative"inflection/noun"
55
require_relative"inflection/parser"
@@ -175,7 +175,6 @@ def plural?(word, term: :noun)
175175
end
176176
module_function:plural?
177177

178-
WHITESPACE_REGEX=/(\s)\s+/.freeze
179178

180179
# Join a list of words into a single sentence
181180
#
@@ -195,19 +194,8 @@ def plural?(word, term: :noun)
195194
# @return [String]
196195
#
197196
# @api public
198-
defjoin_words(*words,separator:", ",conjunctive:"and",final_separator:nil)
199-
oxford_comma=final_separator ||separator
200-
201-
casewords.length
202-
when0
203-
""
204-
when1,2
205-
words.join("#{conjunctive} ").gsub(WHITESPACE_REGEX,"\\1")
206-
else
207-
((words[0...-1]).join("#{separator}") +
208-
"#{oxford_comma}#{conjunctive} " +words[-1])
209-
.gsub(WHITESPACE_REGEX,"\\1").gsub(/\s*(,)/,"\\1")
210-
end
197+
defjoin_words(*words, **options)
198+
CombinedNoun.new(words).join_words(**options)
211199
end
212200
module_function:join_words
213201
end# Inflection
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
moduleStrings
4+
moduleInflection
5+
classCombinedNoun
6+
WHITESPACE_REGEX=/(\s)\s+/.freeze
7+
8+
attr_reader:words
9+
10+
# Create a combined noun
11+
#
12+
# @api private
13+
definitialize(words=[])
14+
@words=words
15+
end
16+
17+
# Combined with another noun
18+
#
19+
# @param [String] word
20+
# the word to combine with
21+
#
22+
# @api public
23+
def +(word)
24+
CombinedNoun.new(@words +[word])
25+
end
26+
27+
# Join a list of words into a single sentence
28+
#
29+
# @example
30+
# CombinedNoun(["one", "two", "three"]).join_words
31+
# # => "one, two and three"
32+
#
33+
# @param [Array[String]] words
34+
# the words to join
35+
# @param [String] separator
36+
# the character to use to join words, defaults to `,`
37+
# @param [String] final_separator
38+
# the separator used before joining the last word
39+
# @param [String] conjunctive
40+
# the word used for combining the last word with the rest
41+
#
42+
# @return [String]
43+
#
44+
# @api public
45+
defjoin_words(separator:", ",conjunctive:"and",final_separator:nil)
46+
oxford_comma=final_separator ||separator
47+
48+
casewords.length
49+
when0
50+
""
51+
when1,2
52+
words.join("#{conjunctive} ").gsub(WHITESPACE_REGEX,"\\1")
53+
else
54+
((words[0...-1]).join(separator.to_s) +
55+
"#{oxford_comma}#{conjunctive} " +words[-1])
56+
.gsub(WHITESPACE_REGEX,"\\1").gsub(/\s*(,)/,"\\1")
57+
end
58+
end
59+
60+
# The combined words
61+
#
62+
# @return [Array[String]]
63+
#
64+
# @api public
65+
defto_ary
66+
@words.to_ary
67+
end
68+
end# CombinedNoun
69+
end# Inflection
70+
end# Strings

‎lib/strings/inflection/noun.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative"combined_noun"
34
require_relative"term"
45
require_relative"nouns"
56

@@ -52,6 +53,18 @@ def plural
5253
find_match(Inflection.configuration.plurals[:noun]) ||
5354
(uncountable? &&word) ||find_match(Nouns.plurals) ||word
5455
end
56+
57+
# Combine this noun with another word
58+
#
59+
# @param [String] other_word
60+
# the other word to combined with
61+
#
62+
# @return [CombinedNoun]
63+
#
64+
# @api public
65+
def +(other_word)
66+
CombinedNoun.new([word,other_word])
67+
end
5568
end# Noun
5669
end# Inflection
5770
end# Strings

‎spec/unit/join_words_spec.rb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@
5050
expect(Strings::Inflection.join_words(" one "," two "," three "))
5151
.toeq(" one, two, and three ")
5252
end
53+
54+
it"joins noun objects"do
55+
joined=Strings::Inflection::Noun("one") +"two" +"three"
56+
expect(joined.join_words).toeq("one, two, and three")
57+
end
5358
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp