diff --git a/lib/yard/tags/types_explainer.rb b/lib/yard/tags/types_explainer.rb index d87657665..d101ed68b 100644 --- a/lib/yard/tags/types_explainer.rb +++ b/lib/yard/tags/types_explainer.rb @@ -32,7 +32,7 @@ def initialize(name) def to_s(singular = true) if name[0, 1] == "#" - singular ? "an object that responds to #{name}" : "objects that respond to #{name}" + (singular ? "an object that responds to " : "objects that respond to ") + list_join(name.split(/ *& */), with: "and") elsif name[0, 1] =~ /[A-Z]/ singular ? "a#{name[0, 1] =~ /[aeiou]/i ? 'n' : ''} " + name : "#{name}#{name[-1, 1] =~ /[A-Z]/ ? "'" : ''}s" else @@ -42,12 +42,12 @@ def to_s(singular = true) private - def list_join(list) + def list_join(list, with: "or") index = 0 list.inject(String.new) do |acc, el| acc << el.to_s acc << ", " if index < list.size - 2 - acc << " or " if index == list.size - 2 + acc << " #{with} " if index == list.size - 2 index += 1 acc end diff --git a/spec/tags/types_explainer_spec.rb b/spec/tags/types_explainer_spec.rb index 8075790de..de7454d3c 100644 --- a/spec/tags/types_explainer_spec.rb +++ b/spec/tags/types_explainer_spec.rb @@ -38,6 +38,18 @@ def parse_fail(types) expect(@t.to_s(false)).to eq "objects that respond to #mymethod" end + it "works for multiple methods joined with '&' (ducktype)" do + @t.name = "#mymethod&#myothermethod&#mythirdmethod" + expect(@t.to_s).to eq "an object that responds to #mymethod, #myothermethod and #mythirdmethod" + expect(@t.to_s(false)).to eq "objects that respond to #mymethod, #myothermethod and #mythirdmethod" + end + + it "works for multiple methods joined with ' & ' (ducktype)" do + @t.name = "#mymethod & #myothermethod & #mythirdmethod" + expect(@t.to_s).to eq "an object that responds to #mymethod, #myothermethod and #mythirdmethod" + expect(@t.to_s(false)).to eq "objects that respond to #mymethod, #myothermethod and #mythirdmethod" + end + it "works for a constant value" do ['false', 'true', 'nil', '4'].each do |name| @t.name = name