Вы можете использовать start_with?
и all?
для этого:
str1 = "The cat is black. jkhdkjhdsjhd"
str2 = "The cat is black and white."
p [str1, str2].all? { |str| str.start_with?('The cat is') } # true
p [str1, str2].all? { |str| str.start_with?('The cat is not') } # false
И, начиная с Ruby 2.5, Enumerable # any ?, all ?, none? и один? Принимая шаблон в качестве аргумента, вы можете передать регулярное выражение, чтобы проверить, начинается ли каждая строка с этой подстроки:
str1 = "The cat is black. jkhdkjhdsjhd"
str2 = "The cat is black and white."
str3 = "renuncia Piñera The cat is black and white."
p [str1, str2].all?(/\AThe cat is /) # true
p [str1, str2, str3].all?(/\AThe cat is /) # false
После просмотра вопроса в комментариях это может сработать:
str1 = "The cat is black."
str2 = "The cat is black and white."
str3 = "The cat"
def all_substring?(sentences)
length = sentences.min.length
sentences.map { |sentence| sentence[0...length] }.uniq == [sentences.sample[0...length]]
end
p all_substring?([str1, str2, str3]) # true
Если вы не знаете заранее, есть ли подстрока, что искать, я думаю, вы можете использовать самое маленькое предложение в качестве подстроки.